试图读取JSON文件时扑向构造

发布于 2025-02-07 04:56:45 字数 2142 浏览 2 评论 0原文

我有以下功能:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2025-02-14 04:56:45
string response = await File(appDocPath).readAsString();

您错过了未来

string response = await File(appDocPath).readAsString();

You missed await for a future

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文