Cannot Overwrite File in Flutter

Viewed 3916
  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.

2 Answers

You probably just need to clear the cache.

import 'package:flutter/services.dart';

imageCache.clear();
import 'package:flutter/services.dart'

imageCache.clearLiveImages();

Add this incase it persists.

Related