我有列表列表,我需要从中检索数据。我正在使用 Provider 从 API 获取数据。所以我用 2 Listview.builder 创建了 ExpansionTile,因为我已经读过为了检索数据我需要为每个列表使用一些循环,例如 Listview.builder。但现在它给了我错误
"The getter 'length' was called on null.
Receiver: null
Tried calling: length"
但是当我使用 print 时,数组不为空,所以我不明白为什么会出现这个错误。
我的代码是:
class _StopScreensState extends State<StopScreens> {
List<Stop> stop;
List<Routes> routes;
List <Arrival> arrivals;
@override
void didChangeDependencies() {
stop = Provider.of<List<Stop>>(context).toList();
routes = Provider.of<List<Routes>>(context).toList();
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
//The list of lists
Iterable<Common> merge(List<Arrival> arrivals, List<Stop> stop,
List<Routes> route) sync* {
for (int i = 0; i < arrivals.length; i++) {
var fishingTackle = routes.where((v) => v.mrId == arrivals[i].mrId).toList();
var fishBait = stop.where((v) => v.stId == arrivals[i].stId).toList();
yield Common(
id: arrivals[i].mrId,
typeId: arrivals[i].mrId,
fishingTackle: fishingTackle,
fishBait: fishBait,
time: arrivals[i].taArrivetime,
);
}
}
//the common list for all arrays
List<Common> common = merge(arrivals, stop, routes).toList();
return Scaffold(
appBar: AppBar(
title: Text('Остановки'),
),
body: Provider.value(value: common) == null
? Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: common.length,
itemBuilder: (context, index) {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ExpansionTile(title: Text(common[index].fishingTackle[index].mrTitle),
children: [
ListView.builder(itemCount: stop.length,itemBuilder: (context, index){
return ListTile(
title: Text(common[index].fishBait[index].stTitle),
leading: Text(common[index].time.toString()),
);
这是因为您有一个空列表。
尝试始终使用空列表初始化您的列表,因此您无需处理每个列表的空值。
改变:
List<Stop> stop;
List<Routes> routes;
List <Arrival> arrivals;
到
List<Stop> stop = [];
List<Routes> routes = [];
List <Arrival> arrivals = [];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句