0

Is there a List class that implements ChangeNotifier? Basically if anything is added to the list or the list is rearranged it will call notifyListeners?

dakamojo
  • 1,839
  • 5
  • 21
  • 35

1 Answers1

1

No. But here's one:

class ChangeNotifierList<T> with ListMixin<T>, ChangeNotifier {
  ChangeNotifierList(this._internalList);

  final List<T> _internalList;

  @override
  int get length => _internalList.length;

  @override
  T operator [](int index) {
    return _internalList[index];
  }

  @override
  void operator []=(int index, T value) {
    _internalList[index] = value;
    notifyListeners();
  }

  @override
  set length(int newLength) {
    _internalList.length = newLength;
    notifyListeners();
  }
}
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • And if you're worried about performance (since `ListMixin` can have poor performance for adding elements), you can do the same thing with `DelegatingList`, with a little more code. – spenster Aug 02 '19 at 18:44