每次重新启动代码时,我都可以运行一次writeToDatabase,因此它会添加到列表中一次。但任何对我的按钮的额外点击都无济于事。
当点击我的onPressed按钮时,它有点像";重做";它的功能。或者我不完全确定。我假设它与异步有关,但并没有完全掌握它
或者可能是因为我在使用上下文?我试着把一切都安排到决赛,但没有成功。或者可能是因为";树在摇晃";?它把文件加载到内存中,这就是我一直在修改的文件,而不是每次运行函数后,当点击按钮时,一个新文件?
我得到了我的onPressed:
onPressed: () {
writeToDatabase("wwwwwww", "fff?`!!", context);
},
我得到了writeToDatabase函数:
writeToDatabase(dynamic imagePath, dynamic textValue, context) async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path;
File file = File('$path/database.json');
// File file = await _localFile;
String userJson = await DefaultAssetBundle.of(context).loadString(file.path);
List userData = jsonDecode(userJson);
Map<String, dynamic> toJson = {
'imagePath': imagePath,
'textValue': textValue,
};
userData.add(toJson);
return file.writeAsString(json.encode(userData));
}
谢谢你的帮助,非常感谢!
答案就在我面前,我只是不知道该往哪里看。
String userJson = await DefaultAssetBundle.of(context).loadString(file.path);
必须更改为:
String userJson = await DefaultAssetBundle.of(context).loadString(file.path, cache: false);
事实证明,它一直在查看该文件的兑现版本。