Reputation: 906
i have an
ObservableList<Model> models;
and i listen to changes as follows
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?
});
});
how to get a list of the items that have been added?
and sometimes i get notifications like the following, what does the index actually refer to?
#<ListChangeRecord index: 49, removed: [Instance of 'Model', Instance of 'Model'], addedCount: 19>
to get the added items my guess is
var addedItems = models.getRange(change.index,change.index+change.addedCount);`
but is that actually the right way?
Upvotes: 0
Views: 904
Reputation: 5662
Yes, that's the way you should use. From the ObservableList source code:
/// [...]
///
/// 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>
///
/// [...]
Upvotes: 3