【问题标题】:NoSuchMethodError:The method '[]' was called on null Tried calling :[]("a")NoSuchMethodError:在 null 上调用了方法“[]”尝试调用:[](“a”)
【发布时间】:2020-08-18 11:44:30
【问题描述】:

我正在尝试使用 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
    }
  }
);   

【问题讨论】:

  • 你现在遇到什么错误
  • 你能在 else 中添加什么是 mydata 的值并添加什么是 quizpage 吗?
  • NoSuchMethodError:在 null 上调用了方法“[]”。接收者:null尝试调用:[](“a”)@RavindraKushwaha
  • 基本上我的数据是有问题和答案的 JSON 文件。 [ { "1": "以下代码的输出是什么?\na = \"p\" * 3\nprint(a)", }, { "1": { "a": "pp", “b”:“ppp”,“c”:“3p”,“d”:“p3”}},{“1”:“ppp”,}]

标签: flutter flutter-layout flutter-dependencies flutter-animation flutter-test


【解决方案1】:
List mydata = new List();
 mydata = json.decode( snapshot.data.toString());

Use This and Now Check it will work now

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2021-11-30
    • 2020-05-23
    相关资源
    最近更新 更多