观察一个 ObservableList
Watching an ObservableList
我有一个
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>
///
/// [...]
我有一个
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>
///
/// [...]