我正在使用Flutter构建一个应用程序,您可以在其中将文件添加到应用程序的缓存中。
有2件事不起作用,我不明白为什么。
这里是source
目录列表失败,路径='/data/user/0/com.example.lockedapp/cache/file_picker /'(操作系统错误: 没有这样的文件或目录,errno = 2)
即使我在尝试显示目录之前已经做好了创建目录的预防措施。
bool checkFilesEmpty() {
var cache = new Directory('/data/user/0/com.example.lockedapp/cache/file_picker');
cache.exists().then((resp) => { if (!resp) { cache.create() } }); // creates a new cache directory if it does not exist
return cache.listSync(recursive: false).toList().length != 0; // returns false if it's empty
}
在第31行,首先检查checkFilesEmpty
,然后检查其他所有内容,但仍然出现错误。
奇怪的是,如果我重新启动该应用程序,它将按预期运行。
有什么解决方案吗?我真的很困惑
答案 0 :(得分:0)
将您的 checkFilesEmpty 放入有状态窗口小部件的initState方法中,因此它将在构建窗口小部件之前被调用。
android
您可以将引起错误的语句放在try catch块中。如果没有列表,则可以在catch块中创建一个:
class MyHomeState extends State<MyHome> {
bool filesEmpty;
@override
void initState() {
super.initState();
filesEmpty = checkFilesEmpty();
}
.
. // Your rest of code
.
}