观看ObservableList

时间:2015-01-01 15:57:38

标签: dart dart-polymer

我有一个

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);`

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

1 个答案:

答案 0 :(得分:2)

是的,这就是你应该使用的方式。来自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>
    ///
    /// [...]