flutter - NoSuchMethodError:在 null 上调用了方法“[]”尝试调用:[](“a”)
问题描述
我正在尝试使用 JSON 文件在颤振中构建一个测验应用程序,所以首先我想在这个小部件中加载问题,然后调用测验页面小部件。此代码在大多数情况下都可以正常工作,但有时它并非总是出现此错误示例代码:
@override
Widget build(BuildContext context) {
// TODO: implement build
// this function is called before the build so that
// the string assettoload is avialable to the DefaultAssetBuilder
setasset();
// and now we return the FutureBuilder to load and decode JSON
return FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(assettoload, cache: true),
builder: (context , snapshot){
List mydata = json.decode( snapshot.data.toString());
if(mydata==null){
return Scaffold(
body: Center(
child: Text(
"Loading",
),
),
);
}
else{
return quizpage(mydata: mydata);//this line giving me the error
}
}
);
解决方案
List mydata = new List();
mydata = json.decode( snapshot.data.toString());
使用这个,现在检查它现在可以工作
推荐阅读
- python - 数字和字母随机混合的正则表达式
- python-3.x - 在Python3中的许多文档中增加子字符串中的数值
- c# - 使用 fetch 和 OnPost (asp.net)
- mongodb - 失败:使用 docker 进行 mongorestore 时的统计信息
- c# - SignalR 替代方案,浏览器/Win32 兼容
- git - Git错误更新被拒绝,因为您当前分支的尖端落后
- node.js - ParcelJS v2 少错误:未启用内联 JavaScript。它在您的选项中设置吗?
- github-pages - 是否可以从子文件夹 HTML 文件创建 GitHub 页面?
- node.js - 我如何在子文档数组节点猫鼬中推送多个对象
- r - 我在 R 中安装 pysch 包时遇到问题