gpt4 book ai didi

dart - 观看一个ObservableList

转载 作者:行者123 更新时间:2023-12-03 04:30:43 27 4
gpt4 key购买 nike

我有一个

ObservableList<Model> models;

我听以下更改
models.listChanges.listen((changes){
changes.forEach((change){
print(change);
List<Model> removedItems = change.removed; //a list of removed objects
//how to get a list of the items that have been added?
});
});

如何获取已添加项目的列表?

有时我会收到类似以下的通知,索引实际上指的是什么?
#<ListChangeRecord index: 49, removed: [Instance of 'Model', Instance of 'Model'], addedCount: 19>

得到我猜是增加的项目
var addedItems = models.getRange(change.index,change.index+change.addedCount);`

但这实际上是正确的方法吗?

最佳答案

是的,这就是您应该使用的方式。从ObservableList源代码:

    /// [...]
///
/// Each list change record contains information about an individual mutation.
/// The records are projected so they can be applied sequentially. For
/// example, this set of mutations:
///
/// var model = new ObservableList.from(['a', 'b']);
/// model.listChanges.listen((records) => records.forEach(print));
/// model.removeAt(1);
/// model.insertAll(0, ['c', 'd', 'e']);
/// model.removeRange(1, 3);
/// model.insert(1, 'f');
///
/// The change records will be summarized so they can be "played back", using
/// the final list positions to figure out which item was added:
///
/// #<ListChangeRecord index: 0, removed: [], addedCount: 2>
/// #<ListChangeRecord index: 3, removed: [b], addedCount: 0>
///
/// [...]

关于dart - 观看一个ObservableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27732284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com