gpt4 book ai didi

dart - Flutter/Dart 覆盖 getApplicationDocumentsDirectory 中的文件不能立即工作

转载 作者:IT王子 更新时间:2023-10-29 06:58:40 24 4
gpt4 key购买 nike

我正在尝试覆盖我的应用程序临时目录中的文件,但由于某种原因,直到我完全热重启我的应用程序,覆盖才会生效。

我试图将我的 _pickedImage 变量设置为新的 changedImage,一旦它被复制到目录中,但是当使用 setState 时,它​​总是保留放置到目录中的第一个图像,而不是每次都覆盖它。因此,当我显示 _pickedImage 时,它​​将始终显示第一个初始图像,直到我完全重新启动,一旦我完全重新启动应用程序,就会发生更改。想要这样做的原因是用户可以根据需要有效地更改图像。希望这是有道理的任何帮助将不胜感激

var image = await ImagePicker.pickImage(source: source, maxWidth: 800.0);

if (image != null) {

final Directory extDir = await getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/image';

if (Directory(dirPath).existsSync()) {
print('it exists');
var dir = new Directory(dirPath);
dir.deleteSync(recursive: true);
if (Directory(dirPath).existsSync()) {
print('still exists');
} else {
//It is getting in here so seemingly its deleting the orignal directory
print('does not exist');
}
}

new Directory(dirPath).createSync(recursive: true);
String path =
'$dirPath/temporaryImage.jpg';

File changedImage = image.copySync(path);`


setState(() {
//this is where the problem lies
_pickedImage = changedImage;
});

最佳答案

缓存是问题所在。请参阅问题 https://github.com/flutter/flutter/issues/24858

来自文档 https://api.flutter.dev/flutter/painting/ImageProvider-class.html

ImageProvider uses the global imageCache to cache images.

你可以使用

import 'package:flutter/painting.dart'

// to clear specific cache
imageCache.evict(FileImage(processedImage));

// to clear all cache
imageCache.clear();

它并没有直接解决我遇到的问题,但是,它是关键。

关于dart - Flutter/Dart 覆盖 getApplicationDocumentsDirectory 中的文件不能立即工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com