dango sjv
dango sjv

Reputation: 173

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.

Upvotes: 6

Views: 5549

Answers (2)

Fredrick
Fredrick

Reputation: 81

import 'package:flutter/services.dart'

imageCache.clearLiveImages();

Add this incase it persists.

Upvotes: 1

Mia
Mia

Reputation: 145

You probably just need to clear the cache.

import 'package:flutter/services.dart';

imageCache.clear();

Upvotes: 10

Related Questions