如何解决“ NoSuchMethodError:在空值上调用吸气剂”长度”

时间:2019-08-24 18:21:13

标签: flutter

我如何解决此问题 我使用函数从api获取数据,但看到错误'NoSuchMethodError:在空值上调用了getter'length'

我的代码:

Future getData() async{
http.Response response = await http.get('https://myappres.000webhostapp.com/pubg/api.php?action=getskin');
debugPrint(response.body);

_data = json.decode(response.body);
_list = _data['categorys'];

return _list;                                                               
}

 Center(
          child: _list.length != null? ListView.builder(
              itemCount: _list.length,
              padding: const EdgeInsets.all(15.9),
              itemBuilder: (BuildContext context, int position){
                final index = position;
                return ListTile(
                    title: Text('${_list[index]['name']}'),
                    subtitle: Image.network('${_list[index]['image']}',width: 200,)
                );
              }
          ):Container()
        )

这是结果错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试使用FutureBuilder等待未来:

FutureBuilder(
      future: getData(),
      builder: (BuildContext context,AsyncSnapshot<List> snapshot){
        if(snapshot.hasData){
           return Center(child: snapshot.length)
        } else return Container();
      },

//you can use too:
getData().then((listData){
      Center(child: listData)...
});