【发布时间】:2020-12-22 09:21:51
【问题描述】:
我想为时间线构建一个ListView 的“消息卡”。消息数据来自具有多个过滤功能的 Container 类。我在示例中只展示了一个。
编辑:澄清 我目前使用 ListView.builder() 让这个 ListView 工作,这取决于能够通过索引访问源数据中的元素。
我想更改它,以便元素来自一个可迭代对象,该可迭代对象在用户滚动列表视图时按需生成。
在当前的工作解决方案中,Provider 有一个 findByChildId,它返回一个 List。此列表通常很长,用户可能只想查看最后 100 条左右的消息。
因此,为了不扫描数千条消息以返回数千条消息,我想象使用 findByChildId 会在用户滚动时产生项目。
class MessageData with ChangeNotifier {
List<MessageRec> _messages;
// New Iterable to lazily search only for items the user wants to look at
Iterable<MessageRec> filterByChildId({String childId}) sync* {
if (childId == null) {
throw 'insanity check - Cannot search for null Child ID';
}
String previous;
if (_messages != null) {
for (MessageRec r in _messages) {
if (r.properties['thread'] == childId) {
if (previous == null || ymdFromDt(r.timeSent) != previous) {
yield DateMark.fromDateString(previous); // Special Message inserted
previous = ymdFromDt(r.timeSent);
}
yield r;
}
}
}
}
// And many other supporting methods here.
}
Note: I assume the above works - this is my first forray into Iterables in Dart / flutter.
The above used to be a method that returned a list of MessageRec by iterating over the entire list and returning all matching items.
The ListView.builder could get items from the resulting list because the elements of a List can be accessed by index.
```dart
... Timeline Widget ...
@override
Widget build(BuildContext context) {
return Consumer<MessageData>(
builder: (context, messageData, _) {
return ListView.builder((context, itemIndex) {
// How to access elements from messageData.filterByChildId(childId) here
MessageRec nextMessage = messageData.filterByChildId(childId)[itemIndex];
// The above does not work because there is no indexing on Iterables
return MessageCard.fromMessageRec(nextmessage);
}
);
}
);
}
... snip rest of Widget methods
或者答案可能在于使用带有 children 的普通 ListView 来自迭代器,但我认为这仍然会最终创建列表中的所有消息。
【问题讨论】:
-
仔细阅读Iterable官方文档
-
如果可以请给出答案。我已阅读文档。谢谢。
-
您没有看到
Iterable.toList/Iterable.elementAt方法? -
这只会解决问题。然后我仍然会使用列表。我的问题可能不清楚 - 我试图避免使用列表。我将更新问题以表明这一点。
-
我正在尝试弄清楚 elementAt 到底是做什么的。从源代码看来,每次调用都会重新开始迭代。这比只使用 toList() 更糟糕!
标签: flutter asynchronous dart iterator