getter 'length' 被调用为 null。接收器:空。如何解决列表列表的这个错误?

时间:2021-07-06 07:34:01

标签: flutter dart

我有列表列表,我需要从中检索数据。我正在使用 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()),
                        );

2 个答案:

答案 0 :(得分:0)

您似乎没有将任何内容分配给 Two Five Eight Twenty Dollar Yuan Rupee USDT

答案 1 :(得分:0)

这是因为你有一个空列表。

尽量用空列表初始化你的列表,这样你就不需要为每个列表处理空值。

变化:

List<Stop> stop;
List<Routes> routes;
List <Arrival> arrivals;

List<Stop> stop = [];
List<Routes> routes = [];
List <Arrival> arrivals = [];