为什么我的小部件的构建方法认为我正在返回 NULL?
Why does my widget's build method think I am returning NULL?
我不明白为什么会出现错误。我所有的 if/else 路径总是 return 一些东西,但是代码似乎认为有一条路径以 NULL?
结尾
错误:
The following _TypeError was thrown building:
type 'Null' is not a subtype of type 'Widget'
@override
Widget build(BuildContext context) {
final messageDao = MessageDao(groupIDPath: widget.groupIDPath);
var msg;
return Scaffold(
body: Container(
child: ListView.builder(
itemCount: data?.length ?? 0,
itemBuilder: (context, index) {
return data?.forEach((key, value) { //ERROR POINTS HERE
msg = Message.fromJson(value);
dataList?.add(msg);
if (dataList?[index] == null) {
return Text('dataList[index] is NULL');
} else {
var receivedType = dataList?[index].type.toString();
var receivedUid = dataList?[index].uid.toString();
var receivedTimestamp = dataList?[index].timestamp.toString();
var receivedText = dataList?[index].text.toString();
var receivedFilterID = dataList?[index].filterID.toString();
var receivedM2URL = dataList?[index].m2URL.toString();
if (dataList?[index].type == '1') { //message type 1
return messageDao.m1Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedText!);
} else if (dataList?[index].type == '2') { //message type 2
return messageDao.m2Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedFilterID!, receivedM2URL!);
} else {
return Text('UNKNOWN MESSAGE TYPE!');
}
}
});
}
),
)
);
}
对于每个 void 函数,它 returns 什么都没有。
https://api.dart.dev/stable/2.16.2/dart-core/Iterable/forEach.html
获取您的数据 List
在返回数据之前添加这一行
var dataList = data as List<Modal>; // use your chat data modal inside list
完整代码
@override
Widget build(BuildContext context) {
final messageDao = MessageDao(groupIDPath: widget.groupIDPath);
var msg;
return Scaffold(
body: Container(
child: ListView.builder(
itemCount: data?.length ?? 0,
itemBuilder: (context, index) {
var dataList = data as List<Modal>; // use your chat data modal inside list
return data?.forEach((key, value) { //ERROR POINTS HERE
msg = Message.fromJson(value);
dataList?.add(msg);
if (dataList?[index] == null) {
return Text('dataList[index] is NULL');
} else {
var receivedType = dataList[index].type.toString();
var receivedUid = dataList[index].uid.toString();
var receivedTimestamp = dataList[index].timestamp.toString();
var receivedText = dataList[index].text.toString();
var receivedFilterID = dataList[index].filterID.toString();
var receivedM2URL = dataList[index].m2URL.toString();
if (dataList?[index].type == '1') { //message type 1
return messageDao.m1Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedText!);
} else if (dataList?[index].type == '2') { //message type 2
return messageDao.m2Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedFilterID!, receivedM2URL!);
} else {
return Text('UNKNOWN MESSAGE TYPE!');
}
}
});
}
),
)
);
}
我不明白为什么会出现错误。我所有的 if/else 路径总是 return 一些东西,但是代码似乎认为有一条路径以 NULL?
结尾错误:
The following _TypeError was thrown building: type 'Null' is not a subtype of type 'Widget'
@override
Widget build(BuildContext context) {
final messageDao = MessageDao(groupIDPath: widget.groupIDPath);
var msg;
return Scaffold(
body: Container(
child: ListView.builder(
itemCount: data?.length ?? 0,
itemBuilder: (context, index) {
return data?.forEach((key, value) { //ERROR POINTS HERE
msg = Message.fromJson(value);
dataList?.add(msg);
if (dataList?[index] == null) {
return Text('dataList[index] is NULL');
} else {
var receivedType = dataList?[index].type.toString();
var receivedUid = dataList?[index].uid.toString();
var receivedTimestamp = dataList?[index].timestamp.toString();
var receivedText = dataList?[index].text.toString();
var receivedFilterID = dataList?[index].filterID.toString();
var receivedM2URL = dataList?[index].m2URL.toString();
if (dataList?[index].type == '1') { //message type 1
return messageDao.m1Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedText!);
} else if (dataList?[index].type == '2') { //message type 2
return messageDao.m2Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedFilterID!, receivedM2URL!);
} else {
return Text('UNKNOWN MESSAGE TYPE!');
}
}
});
}
),
)
);
}
对于每个 void 函数,它 returns 什么都没有。 https://api.dart.dev/stable/2.16.2/dart-core/Iterable/forEach.html
获取您的数据 List
在返回数据之前添加这一行
var dataList = data as List<Modal>; // use your chat data modal inside list
完整代码
@override
Widget build(BuildContext context) {
final messageDao = MessageDao(groupIDPath: widget.groupIDPath);
var msg;
return Scaffold(
body: Container(
child: ListView.builder(
itemCount: data?.length ?? 0,
itemBuilder: (context, index) {
var dataList = data as List<Modal>; // use your chat data modal inside list
return data?.forEach((key, value) { //ERROR POINTS HERE
msg = Message.fromJson(value);
dataList?.add(msg);
if (dataList?[index] == null) {
return Text('dataList[index] is NULL');
} else {
var receivedType = dataList[index].type.toString();
var receivedUid = dataList[index].uid.toString();
var receivedTimestamp = dataList[index].timestamp.toString();
var receivedText = dataList[index].text.toString();
var receivedFilterID = dataList[index].filterID.toString();
var receivedM2URL = dataList[index].m2URL.toString();
if (dataList?[index].type == '1') { //message type 1
return messageDao.m1Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedText!);
} else if (dataList?[index].type == '2') { //message type 2
return messageDao.m2Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedFilterID!, receivedM2URL!);
} else {
return Text('UNKNOWN MESSAGE TYPE!');
}
}
});
}
),
)
);
}