Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Overwrite File in Flutter

  Future<Null> pickImageFromGallery() async {
    String path = (await getApplicationDocumentsDirectory()).path;
    File imageExist = new File(path + '/image1.png');
    if(await imageExist.exists()) {
      imageExist.delete();
    }

    File imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
    if(imageFile == null) return;
    File newImage = await imageFile.copy('$path/image1.png');
    setState(() {
      this.categoryIcon = newImage;
    });
  }

I'm creating an application that allow user to choose an icon for item. I'm using Image Picker to allow user to choose an image. When the user choose an image, i want to overwrite the file in the app directory.

But with that code, i got the same File Image every time i choose a new image. It seems like the image can't be replaced.

like image 408
dango sjv Avatar asked Mar 03 '23 19:03

dango sjv


1 Answers

You probably just need to clear the cache.

import 'package:flutter/services.dart';

imageCache.clear();
like image 131
Mia Avatar answered Mar 06 '23 09:03

Mia