我收到此错误 getter 'length' was called on null Receiver: null : 尝试调用:length I/flutter
Posted
技术标签:
【中文标题】我收到此错误 getter \'length\' was called on null Receiver: null : 尝试调用:length I/flutter【英文标题】:I get this error The getter 'length' was called on null Receiver: null : Tried calling: length I/flutter我收到此错误 getter 'length' was called on null Receiver: null : 尝试调用:length I/flutter 【发布时间】:2020-12-15 03:45:52 【问题描述】: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/flutter (15602): ══╡ 小部件库发现异常╞═════════════════════ ══════════════════════════════════════ I/flutter (15602):在构建 TripActivityList(dirty) 时引发了以下 NoSuchMethodError: I/flutter (15602):在 null 上调用了 setter 'length='。 I/颤振(15602):接收器:空 I/flutter (15602):尝试调用:长度=0 我/颤振(15602): I/flutter (15602):相关的导致错误的小部件是: 我/颤动(15602):TripActivityList I/flutter (15602): file:///C:/Users/Mamadou%20Lamine%20Bouss/uvs_voyage/lib/views/trip/widgets/trip_activities.dart:35:19 我/颤振(15602): I/flutter (15602):抛出异常时,这是堆栈: I/flutter (15602): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) I/flutter (15602): #1 TripActivityList.build (package:uvs_voyage/views/trip/widgets/trip_activity_list.dart:11:31) I/flutter (15602): #2 StatelessElement.build (package:flutter/src/widgets/framework.dart:4620:28) I/flutter (15602): #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15) I/flutter (15602): #4 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #5 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) I/flutter (15602): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) I/flutter (15602): #8 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #9 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) I/flutter (15602): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #11 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14) I/flutter (15602): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #13 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14) I/flutter (15602): #14 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #15 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) I/flutter (15602): #16 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #17 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) I/flutter (15602): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #19 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) I/flutter (15602): #20 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #21 ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5) I/flutter (15602): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
【问题讨论】:
【参考方案1】:当 Widget 被实例化时,列表activities
可能为空。
当data == null时,你需要提防这种情况
itemCount: activities!= null ? activities.length : 0,
【讨论】:
但我没有收到任何数据 您可能必须将其转换为可以刷新数据的有状态小部件,并在 initState 中将参数设置为 widget.activites以上是关于我收到此错误 getter 'length' was called on null Receiver: null : 尝试调用:length I/flutter的主要内容,如果未能解决你的问题,请参考以下文章
组件异常:组件的视图配置 getter 回调必须是函数(收到“未定义”)
不变违规:组件`div`的视图配置getter回调必须是一个函数(收到`undefined`)
发布 http 请求:在 null 颤动上调用了 getter 'length'