试图读取JSON文件时扑向构造
我有以下功能:
void readJSON(String path) async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path + '/' + path;
print('Trying to read json file from ' + appDocPath);
final Future<String> response = File(appDocPath).readAsString();
final data = await json.decode(response.toString());
final coordinates = await (data['elements'] as List).cast<double>();
for(int i = 0; i <= coordinates.length; ++i){
print(i);
}
}
如您所见,我正在迭代每条JSON系列以打印它并查看其包含的内容。
[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 1)
[ ] E/flutter ( 8316): Instance of 'Future<String>'
我到底在哪里?
输出:
[ +81 ms] E/flutter (10493): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
[ ] E/flutter (10493):
[ ] E/flutter (10493): ^
[ ] E/flutter (10493):
[ ] E/flutter (10493): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
[ ] E/flutter (10493): #1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:501:7)
[ ] E/flutter (10493): #2 _parseJson (dart:convert-patch/convert_patch.dart:36:10)
[ ] E/flutter (10493): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
[ ] E/flutter (10493): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
[ ] E/flutter (10493): #5 file.readJSON (package:camera_example/file.dart:50:29)
[ ] E/flutter (10493): <asynchronous suspension>
[ ] E/flutter (10493):
[ +5 ms] E/flutter (10493): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
[ ] E/flutter (10493): #0 file.readJSON (package:camera_example/file.dart:52:36)
[ ] E/flutter (10493): <asynchronous suspension>
[ ] E/flutter (10493):
I have the following function:
void readJSON(String path) async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path + '/' + path;
print('Trying to read json file from ' + appDocPath);
final Future<String> response = File(appDocPath).readAsString();
final data = await json.decode(response.toString());
final coordinates = await (data['elements'] as List).cast<double>();
for(int i = 0; i <= coordinates.length; ++i){
print(i);
}
}
As you can see, I am iterating through every JSON line to print it and see what it contains.
[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 1)
[ ] E/flutter ( 8316): Instance of 'Future<String>'
Where exactly am I going wrong?
Output:
[ +81 ms] E/flutter (10493): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
[ ] E/flutter (10493):
[ ] E/flutter (10493): ^
[ ] E/flutter (10493):
[ ] E/flutter (10493): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
[ ] E/flutter (10493): #1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:501:7)
[ ] E/flutter (10493): #2 _parseJson (dart:convert-patch/convert_patch.dart:36:10)
[ ] E/flutter (10493): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
[ ] E/flutter (10493): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
[ ] E/flutter (10493): #5 file.readJSON (package:camera_example/file.dart:50:29)
[ ] E/flutter (10493): <asynchronous suspension>
[ ] E/flutter (10493):
[ +5 ms] E/flutter (10493): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
[ ] E/flutter (10493): #0 file.readJSON (package:camera_example/file.dart:52:36)
[ ] E/flutter (10493): <asynchronous suspension>
[ ] E/flutter (10493):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错过了未来
You missed await for a future