【问题标题】:Cannot Overwrite File in Flutter无法在 Flutter 中覆盖文件
【发布时间】:2019-11-10 22:42:38
【问题描述】:
  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;
    });
  }

我正在创建一个允许用户为项目选择图标的应用程序。我正在使用图像选择器来允许用户选择图像。当用户选择图像时,我想覆盖应用目录中的文件。

但是使用该代码,我每次选择新图像时都会得到相同的文件图像。图片好像不能换了。

【问题讨论】:

标签: android image file flutter io


【解决方案1】:

您可能只需要清除缓存。

import 'package:flutter/services.dart';

imageCache.clear();

【讨论】:

  • 完美答案。
【解决方案2】:
import 'package:flutter/services.dart'

imageCache.clearLiveImages();

添加这个以防它仍然存在。

【讨论】:

    猜你喜欢
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2017-09-20
    • 2014-06-19
    • 1970-01-01
    • 2021-12-02
    • 2018-12-30
    相关资源
    最近更新 更多