Cannot Overwrite File in Flutter

3,102

You probably just need to clear the cache.

import 'package:flutter/services.dart';

imageCache.clear();
Share:
3,102
dango sjv
Author by

dango sjv

Updated on December 12, 2022

Comments

  • dango sjv
    dango sjv over 1 year
      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.

  • Rabel Ahmed
    Rabel Ahmed over 2 years
    Perfect answer.
  • Madhav Mishra
    Madhav Mishra about 2 years
    What if it is a file document like output.xlsx instead of image1.png in the app directory? How can I clear them?