首页 > 解决方案 > Listview 构建器区域上的“NoSuchMethodError:getter 'length' 被调用为 null”

问题描述

我想问一下我遇到的错误。所以在有人声称这个问题与其他人相似之前,也许是的,但其他人的回答仍然无法解决我的问题。

所以,我的代码有问题,错误显示大约 5 秒,然后显示数据列表。然后我搜索解决方案,我找到了 futurebuilder 的答案,但是当我调试时,错误仍然显示并且它是永久性的,而不是像以前那样更改为数据列表。这是我的代码

  List data;

  Future<String> loadJsonData() async {
    var jsonText = await rootBundle.loadString('Json/indomodel.json');
    if(this.mounted){
    setState(() {
      data = json.decode(jsonText);
    });
    }

    //print(jsonText);
  }

  @override
  void initState() {
    super.initState();
    this.loadJsonData();
    //loaddata();
  }

  @override
  Widget build(BuildContext context) {
    //var buildContext = (BuildContext context, AsyncSnapshot snapshot);
    return Scaffold(
      resizeToAvoidBottomInset: false,
      resizeToAvoidBottomPadding: false,
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
              height: 60.0,
              width: MediaQuery.of(context).size.width,
              decoration: BoxDecoration(color: Colors.white),
              child: TextField(
                decoration: InputDecoration(
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.transparent),
                    ),
                    alignLabelWithHint: true,
                    focusColor: Colors.white,
                    hintText: "Ketik Kata",
                    hintStyle: TextStyle(color: Colors.grey),
                    //labelText: "Bahasa Indonesia",
                    icon: Icon(
                      FontAwesomeIcons.search,
                      color: Colors.grey,
                      size: 15.0,
                    ),
                    labelStyle: TextStyle(color: Colors.grey)),
                cursorColor: Colors.yellow,
                style: TextStyle(color: Colors.grey.shade800),
              ),
            ),
            Container(
                padding: EdgeInsets.only(top: 10.0),
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height * 0.6,
                child: FutureBuilder(
                  future: loadJsonData(),
                  builder: (BuildContext context, AsyncSnapshot snapshot){
                    if(!snapshot.hasData)return Center(child: CircularProgressIndicator(),);
                    if(snapshot.connectionState == ConnectionState.done){
                      if(snapshot.hasError) return Text('Error ${snapshot.error}');
                      return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, index){
                    return Card(
                      elevation: 0.3,
                        child: ListTile(
                     // leading: CircleAvatar(child: Icon(FontAwesomeIcons.adjust),),
                      title: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[

                                SelectableText(
                                  data[index]['word'],
                                  style: TextStyle(
                                      fontSize: 16.0,
                                      fontWeight: FontWeight.bold,
                                      color: Colors.deepPurple.shade800),
                                ),
                                Text(
                                  "My Kamus",
                                  style: TextStyle(
                                      color: Colors.grey, fontSize: 10),
                                )
                              ],
                            ),
                            SizedBox(height: 10.0,),
                            Text(data[index]['trans'], style: TextStyle(color: Colors.grey.shade700, fontSize: 13.0),)
                          ],
                        ),
                    ),
                    );
                  },
                );
                    }
                  },
                )
                )
          ],
        ),
      ),
    );

标签: flutter

解决方案


尝试将您的替换itemCount: snapshot.data.length,itemCount: snapshot.data == null ? 0 : snapshot.data.length,. 那应该解决它。


推荐阅读