【发布时间】:2020-12-18 12:57:13
【问题描述】:
我在 flutter 目录中有一个 JSON 文件,而不是 assets。
json_data.json:-
{
"foo" : "bar"
}
我想在不同的文件上读取这个 JSON。
喜欢
myfile.dart:-
import "package:mypackage/json_data.json" as data;
import 'dart:convert';
var my_data = json.decode(data);
我收到错误:-
The name 'data' refers to an import prefix, so it must be followed by '.'.
Try correcting the name to refer to something other than a prefix, or renaming the prefix.
这里有什么问题?为什么我不能从本地导入中读取 JSON?
【问题讨论】:
-
因为
json.decode()将string作为源。我建议使用readAsString将 JSON 文件作为字符串读取,然后传递给json.decode