Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于JSON.decode的dart中的NosuchMethod_Json_Dart - Fatal编程技术网

用于JSON.decode的dart中的NosuchMethod

用于JSON.decode的dart中的NosuchMethod,json,dart,Json,Dart,我正在尝试从dart中的文件解析JSON。我已经设法读取并打印了该文件,但当我试图将其转换为JSON时,它会抛出错误 file.dart: import 'dart:convert' as JSON; import 'dart:io'; main() { final jsonAsString = new File('./text.json').readAsString().then(print); //Successful printing of json final json

我正在尝试从dart中的文件解析JSON。我已经设法读取并打印了该文件,但当我试图将其转换为JSON时,它会抛出错误

file.dart:

import 'dart:convert' as JSON;
import 'dart:io';

main() {
    final jsonAsString = new File('./text.json').readAsString().then(print); //Successful printing of json
    final json = JSON.decode(jsonAsString);


    print('$json');
}
错误:

Unhandled exception:
NoSuchMethodError: No top-level method 'JSON.decode' declared.
Receiver: top-level
Tried calling: JSON.decode(Instance of '_Future')
#0      NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:196)
#1      main (file:///Users/ninanjohn/Trials/dartTrials/stubVerifier.dart:7:15)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:265)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:151)
未处理的异常:
NoSuchMethodError:未声明顶级方法“JSON.decode”。
接收器:顶级
尝试调用:JSON.decode('\u Future'的实例)
#0 NoSuchMethodError.\u throwNew(省道:核心补丁/错误补丁。省道:196)
#1主要(file:///Users/ninanjohn/Trials/dartTrials/stubVerifier.dart:7:15)
#2_星状。(省道:隔离补片/隔离补片。省道:265)
#3 RawReceivePortImpl.handleMessage(dart:isolate patch/isolate_patch.dart:151)

我在这里遗漏了什么或做错了什么?

首先,在导入
dart:convert
库时,您给它一个名为
JSON
的别名。这意味着你必须像这样使用它

JSON.JSON.decode(jsonString);
我想您应该使用
show
而不是
as
。有关更多详细信息,请参见问题SO

另一个问题是这条线,

final jsonAsString = new File('./text.json').readAsString().then(print);
实际上,您没有为该变量指定
字符串
,因为
.then()
方法返回一个
未来

或者同步读取文件

final jsonAsString = new File('./text.json').readAsStringSync();
print(jsonAsString);
final json = JSON.decode(jsonAsString);
print('$json');
或者把它改成

new File('./text.json').readAsString().then((String jsonAsString) {
  print(jsonAsString);
  final json = JSON.decode(jsonAsString);
  print('$json');
});

首先,在导入
dart:convert
库时,给它一个名为
JSON
的别名。这意味着你必须像这样使用它

JSON.JSON.decode(jsonString);
我想您应该使用
show
而不是
as
。有关更多详细信息,请参见问题SO

另一个问题是这条线,

final jsonAsString = new File('./text.json').readAsString().then(print);
实际上,您没有为该变量指定
字符串
,因为
.then()
方法返回一个
未来

或者同步读取文件

final jsonAsString = new File('./text.json').readAsStringSync();
print(jsonAsString);
final json = JSON.decode(jsonAsString);
print('$json');
或者把它改成

new File('./text.json').readAsString().then((String jsonAsString) {
  print(jsonAsString);
  final json = JSON.decode(jsonAsString);
  print('$json');
});