ImagePicker, how to set image again?

前端 未结 1 1755
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-27 23:18

Minimal Code:

File _file;

Future _pickImage() async {
  final image = await ImagePicker.pickImage(source: ImageSource.camera);

  i         


        
相关标签:
1条回答
  • 2021-01-28 00:10

    you need 3 things:

    first you have to use ImageProvider and its evict() method:

    var image = FileImage(File('someImage.jpg'));
    

    then you need Image widget that uses above ImageProvider and also assigns a unique key in order to be "different" each time build() method is called:

    child: Image(
      image: image,
      key: UniqueKey(),
    ),
    

    and finally after you overwrite someImage.jpg you have to call evict() method:

    // part of your _pickImage() method
    // here someImage.jpg contains updated content
    image.evict();
    setState(() {});
    

    UPDATE: actually you dont need var image = FileImage(File('someImage.jpg')); - you can use it directly inside Image widget as image: FileImage(File('someImage.jpg')) and call FileImage(File('someImage.jpg')).evict() after your image is ovewritten

    0 讨论(0)
提交回复
热议问题