作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我该如何解决这个问题我使用函数从 api 获取数据,但我看到错误“NoSuchMethodError:getter 'length' was called on null'
我的代码:
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()
)
最佳答案
尝试使用 FutureBuilder 等待 Future:
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)...
});
关于flutter - 如何修复 'NoSuchMethodError: The getter ' 长度' was called on null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57640535/
我是一名优秀的程序员,十分优秀!