flutter - 如何修复'NoSuchMethodError:getter'length'被调用为null'
问题描述
我如何解决这个问题我使用函数从 api 获取数据,但我看到错误'NoSuchMethodError: The 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 等待未来:
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)...
});
推荐阅读
- c# - 如何使用windows账号登录网站?
- reactjs - ASP.net Core 2.2 上的 Reactjs 到 Webapi 失败,应用程序中出现错误 415,但 Postman 中没有
- angular - NGRX 8,角度,如何处理未定义的错误
- c++ - 摆脱 TCP 代理中的 boost 依赖。boost::system::error_code 等效
- javascript - 功能组件中的 React Navigation getParams
- swift - 协议可以在采用对象中没有明确实现的情况下定义下标(keyPath:) 吗?
- gulp - 将 Gulp 3 函数转换为 Gulp V4
- python - 给定 N 个元素的列表,如何确定按比例递减的权重?
- aws-lambda - Lambda 推送到 SQS - 同步或异步
- angular - 如何将 URL 动态添加到 Angular Service Worker DataGroup