如何从文件管理器 flutter 解析本地 json 文件?
how do I parse the local json file from file manager flutter?
我看过很多从 assets
解析 json
文件的例子
但是如何从文件管理器获取 json 文件并解析它?
void openJsonFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path!);
if (file.path.contains('.json')) {
parse(file);
}
} else {
// User canceled the picker
}
}
void parse(File file) async {
final String response = await rootBundle.loadString(file.path);
List<JsonItem> items = (jsonDecode(response) as List<dynamic>)
.map((e) => JsonItem.fromJson(e))
.toList();
Navigator.of(dkey.currentState!.context).push(
MaterialPageRoute(
builder: (context) => JsonParsedListScreen(items: items)),
);
}
class JsonItem {
int? id;
String? name;
JsonItem({this.id, this.name});
factory JsonItem.fromJson(Map<String, dynamic> json) {
return JsonItem(name: json['name'], id: json['id']);
}
}
E/flutter (20466): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Unable to load asset: /data/user/0/com.example.play/cache/file_picker/sample.json
E/flutter (20466): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
E/flutter (20466): <asynchronous suspension>
E/flutter (20466): #1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:72:27)
E/flutter (20466): <asynchronous suspension>
E/flutter (20466): #2 HomePage.parse (package:play/nextPage.dart:52:29)
E/flutter (20466): <asynchronous suspension>
E/flutter (20466):
我得到这个日志
对于这一行 final String response = await rootBundle.loadString(file.path);
这是从 assets
、
加载的
final String response = await rootBundle.loadString(file.path);
但是从文件中读取是这样的:
final response = await file.readAsString();
我看过很多从 assets
json
文件的例子
但是如何从文件管理器获取 json 文件并解析它?
void openJsonFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path!);
if (file.path.contains('.json')) {
parse(file);
}
} else {
// User canceled the picker
}
}
void parse(File file) async {
final String response = await rootBundle.loadString(file.path);
List<JsonItem> items = (jsonDecode(response) as List<dynamic>)
.map((e) => JsonItem.fromJson(e))
.toList();
Navigator.of(dkey.currentState!.context).push(
MaterialPageRoute(
builder: (context) => JsonParsedListScreen(items: items)),
);
}
class JsonItem {
int? id;
String? name;
JsonItem({this.id, this.name});
factory JsonItem.fromJson(Map<String, dynamic> json) {
return JsonItem(name: json['name'], id: json['id']);
}
}
E/flutter (20466): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Unable to load asset: /data/user/0/com.example.play/cache/file_picker/sample.json
E/flutter (20466): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
E/flutter (20466): <asynchronous suspension>
E/flutter (20466): #1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:72:27)
E/flutter (20466): <asynchronous suspension>
E/flutter (20466): #2 HomePage.parse (package:play/nextPage.dart:52:29)
E/flutter (20466): <asynchronous suspension>
E/flutter (20466):
我得到这个日志 对于这一行 final String response = await rootBundle.loadString(file.path);
这是从 assets
、
final String response = await rootBundle.loadString(file.path);
但是从文件中读取是这样的:
final response = await file.readAsString();