我正在尝试使用Flutter和Dart将数组持久化为JSON文件。
我有以下代码:
@if(Auth::user()->professionals >='1')
<img src="{{asset('storage/'.Auth::user()->professionals->image_path}}" alt="Image Description">
@else
//Show something different
@endif
在if块中,使用void save(String fileName, Map<String,dynamic> content) async {
final file = await _localFile(fileName);
List<Map<String,dynamic>> contents = [];
if(file.existsSync()) {
// read the file
contents = jsonDecode(file.readAsStringSync());
debugPrint('$contents');
// add new content
contents.add(content);
// save the content
file.writeAsStringSync(jsonEncode(contents));
} else {
// add content to contents
contents.add(content);
// save the file as json
file.writeAsStringSync(jsonEncode(contents));
}
}
时出现错误
JSONDecoder(file.readAsStringSync())
当我清楚地将Map放置在数组中时,为什么说列表。