当前位置: 首页 > 知识库问答 >
问题:

我得到这个错误getter长度被调用空接收器:空:尝试调用:长度I/flutter

濮阳宁
2023-03-14
import '../models/activity_model.dart';
import 'package:flutter/foundation.dart';

   class Trip {
   String id;
   String city;
  String name;
  List<Activity> ;
  DateTime date;
  Trip({
    @required this.name,
    @required this.city,
    @required this.activities,
    @required this.date,
  }) : id = UniqueKey().toString();
}

trip_activityList.dart

import 'package:flutter/material.dart';
import '../../../models/activity_model.dart';

class TripActivityList extends StatelessWidget {
  final List<Activity> activities;
  TripActivityList({this.activities});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: activities.length,
        itemBuilder: (context, index) {
          return Text(activities[index].name);
        });
  }
}

trip_activities.dart

import 'package:flutter/material.dart';
import 'package:uvs_voyage/views/trip/widgets/trip_activity_list.dart';

import '../../../models/activity_model.dart';

class TripActivities extends StatelessWidget {
  final List<Activity> activities;
  TripActivities({this.activities});
  @override
  Widget build(BuildContext context) {
    return Container(
      child: DefaultTabController(
        length: 2,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.blue,
              child: TabBar(
                indicatorColor: Colors.blue[100],
                tabs: <Widget>[
                  Tab(
                    text: 'En cours',
                  ),
                  Tab(
                    text: 'Terminé',
                  ),
                ],
              ),
            ),
            Container(
              height: 600,
              child: TabBarView(
                physics: NeverScrollableScrollPhysics(),
                children: <Widget>[
                  TripActivityList(
                    activities: activities,
                  ),
                  TripActivityList(
                    activities: activities,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

这花费了出乎意料的长时间。)/I/颤振(15602):══╡ WIDGETS库捕获到异常╞═══════════════════════════════════════════════════════════ I/flatter(15602):在生成TripActivityList(dirty)时引发了以下NoSuchMethodError:I/flatter(15602):对null调用了setter“length=”。I/flatter(15602):接收方:null I/flatter(15602):尝试调用:长度=0 I/flatter(15602):I/flatter(15602):相关错误导致小部件为:I/flatter(15602):TripActivityList I/flatter(15602):file:///C:/Users/MamadouLamine Bouss/uvs\u航行/lib/views/trip/widgets/trip\u活动。dart:35:19 I/flatter(15602):I/flatter(15602):抛出异常时,这是堆栈:I/flatter(15602):#0对象。noSuchMethod(dart:core patch/object_patch.dart:51:5)I/颤振(15602):#1个TripActivityList。构建(包:uvs_航行/视图/行程/小部件/行程活动列表。dart:11:31)I/flatter(15602):#2个元素。构建(包:flatter/src/widgets/framework.dart:4620:28)I/flatter(15602):#3组件元素。performRebuild(包:flatter/src/widgets/framework.dart:4546:15)I/flatter(15602):#4元素。重建(包:flatter/src/widgets/framework.dart:4262:5)I/flatter(15602):#5个元素。更新(包:flatter/src/widgets/framework.dart:4627:5)I/flatter(15602):#6元素。updateChild(包:flatter/src/widgets/framework.dart:3245:15)I/flatter(15602):#7组件元素。performRebuild(包:flatter/src/widgets/framework.dart:4571:16)I/flatter(15602):#8元素。重建(包:flatter/src/widgets/framework.dart:4262:5)I/flatter(15602):#9个元素。更新(包:flatter/src/widgets/framework.dart:4627:5)I/flatter(15602):#10元素。updateChild(包:flatter/src/widgets/framework.dart:3245:15)I/flatter(15602):#11 SingleChildRenderObjectElement。更新(包:flatter/src/widgets/framework.dart:5881:14)I/flatter(15602):#12元素。updateChild(包:flatter/src/widgets/framework.dart:3245:15)I/flatter(15602):#13 SingleChildRenderObjectElement。更新(包:flatter/src/widgets/framework.dart:5881:14)I/flatter(15602):#14元素。updateChild(包:flatter/src/widgets/framework.dart:3245:15)I/flatter(15602):#15组件元素。PerformBuild(包:flatter/src/widgets/framework.dart:4571:16)I/flatter(15602):#16元素。重建(包:flatter/src/widgets/framework.dart:4262:5)I/flatter(15602):#17个元素。更新(包:flatter/src/widgets/framework.dart:4627:5)I/flatter(15602):#18元素。updateChild(包:flatter/src/widgets/framework.dart:3245:15)I/flatter(15602):#19组件元素。performRebuild(包:flatter/src/widgets/framework.dart:4571:16)I/flatter(15602):#20元素。重建(包:flatter/src/widgets/framework.dart:4262:5)I/flatter(15602):#21 ProxyElement。更新(包:flatter/src/widgets/framework.dart:4906:5)I/flatter(15602):#22元素。updateChild(包:flatter/src/widgets/framework.dart:3245:15)I/flatter(15602):#23组件元素。PerformBuild(包:flatter/src/widgets/framework.dart:4571:16)

共有1个答案

方寒
2023-03-14

当小部件被实例化时,列表活动可能为空。当data==null时,您需要防止这种情况

 itemCount: activities!= null ? activities.length : 0,
 类似资料: