Dart 将对象另存为编码的JSON文件,然后从文件中提取JSON
我试图将一个对象作为JSON保存在一个保存到缓存的文件中,然后在需要时打开该文件并提取JSON 我用JSON将对象保存为文件的代码如下:Dart 将对象另存为编码的JSON文件,然后从文件中提取JSON,dart,flutter,Dart,Flutter,我试图将一个对象作为JSON保存在一个保存到缓存的文件中,然后在需要时打开该文件并提取JSON 我用JSON将对象保存为文件的代码如下: Future<File> saveJsonFile(FireImage image) async { jsonDirectory = '${(await getApplicationDocumentsDirectory()).path}/json_cache/'; final filePath = '$jsonDirectory${imag
Future<File> saveJsonFile(FireImage image) async {
jsonDirectory = '${(await getApplicationDocumentsDirectory()).path}/json_cache/';
final filePath = '$jsonDirectory${image.name}';
print("saving json to $filePath");
return File(filePath)
..createSync(recursive: true)
..writeAsString(json.encode(image));
}
但是当我到达这一行时,
var-imageJson=json.decode(imageFile.toString())
我得到错误格式异常:意外字符(在字符1处)
使用readAsString[Sync]
读取文件。试着改变
var imageJson = json.decode(imageFile.toString());
到
var imageJson = json.decode(imageFile.toString());
var imageJson = json.decode(imageFile.readAsStringSync());