首页 > 解决方案 > 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
    }
  }
);   

标签: flutterflutter-layoutflutter-dependenciesflutter-animationflutter-test

解决方案


List mydata = new List();
 mydata = json.decode( snapshot.data.toString());

使用这个,现在检查它现在可以工作


推荐阅读