我正试图将列表列表放在ui线程中,但我不确定如何正确地做到这一点。我正试图将小部件放在地图方法中,但得到了错误:
1. The element type 'List<Text>' can't be assigned to the list type 'Widget'
如果我需要同时显示列表列表中的数据,如何避免此错误?
List<Common> common = merge(buying, bait, tackle).toList();
return Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
common[index].fishingTackle.map((e) => Text(e.name)),
common[index].fishBait.map((e) => Text(e.baitName)),
common[index].time.toString()),
],
)
);
}
}
Iterable<Common> merge(List<TimeByuing> buying, List<FishBait> bait,
List<FishingTackle> tackle) sync* {
for (int i = 0; i < buying.length; i++) {
var fishingTackle = tackle.where((v) => v.id == buying[i].id).toList();
var fishBait = bait.where((v) => v.typeId == buying[i].typeId).toList();
yield Common(
id: buying[i].id,
typeId: buying[i].typeId,
fishingTackle: fishingTackle,
fishBait: fishBait,
time: buying[i].time,
);
}
}
因此,Column
小部件需要一个单个小部件的数组。您所拥有的实际上是一个嵌套的小部件阵列。
您可以在map()
上使用排列运算符,使它们在列中工作。
此外,您还需要使用toList()
将map
创建的iterable
转换为列表,因为column
无法使用迭代。
...common[index].fishingTackle.map((e) => Text(e.name)).toList(),
...common[index].fishBait.map((e) => Text(e.baitName)).toList(),
您的另一行common[index].time.toString()),
也需要转换为Text
小部件,因为任何.toString()
的类型都是String
,而不是Widget
您可以通过以下方式完成:
Text(common[index].time.toString())),