Skip to content

Instantly share code, notes, and snippets.

@Taosif7
Last active August 21, 2022 18:17
Show Gist options
  • Save Taosif7/565caaab94afffc1bc25e434f6fdf036 to your computer and use it in GitHub Desktop.
Save Taosif7/565caaab94afffc1bc25e434f6fdf036 to your computer and use it in GitHub Desktop.
A List implementation, that notifies on every change
import 'dart:math';
import 'package:flutter/foundation.dart';
class ListValueNotifier<T> extends ChangeNotifier implements ValueListenable<Iterable<T>>, List<T> {
ListValueNotifier(this._value);
List<T> _value;
@override
Iterable<T> get value => _value;
set value(Iterable<T> newValue) {
if (_value == newValue) return;
_value = newValue.toList();
notifyListeners();
}
@override
add(T element) {
_value.add(element);
notifyListeners();
}
@override
bool remove(element) {
var removed = _value.remove(element);
notifyListeners();
return removed;
}
@override
T removeAt(int index) {
var v = _value.removeAt(index);
notifyListeners();
return v;
}
@override
void clear() {
_value.clear();
notifyListeners();
}
@override
insert(int index, T element) {
_value.insert(index, element);
notifyListeners();
}
@override
void insertAll(int index, Iterable<T> iterable) {
_value.insertAll(index, iterable);
notifyListeners();
}
@override
List<T> operator +(List<T> other) {
return _value + other;
}
@override
T operator [](int index) {
return _value[index];
}
@override
void operator []=(int index, T value) {
_value[index] = value;
notifyListeners();
}
@override
void addAll(Iterable<T> iterable) {
_value.addAll(iterable);
notifyListeners();
}
@override
bool any(bool Function(T element) test) {
return _value.any(test);
}
@override
Map<int, T> asMap() {
return _value.asMap();
}
@override
List<R> cast<R>() {
return _value.cast<R>();
}
@override
bool contains(Object? element) {
return _value.contains(element);
}
@override
T elementAt(int index) {
return _value.elementAt(index);
}
@override
bool every(bool Function(T element) test) {
return _value.every(test);
}
@override
Iterable<M> expand<M>(Iterable<M> Function(T element) toElements) {
return _value.expand(toElements);
}
@override
void fillRange(int start, int end, [T? fillValue]) {
_value.fillRange(start, end, fillValue);
notifyListeners();
}
@override
T firstWhere(bool Function(T element) test, {T Function()? orElse}) {
return _value.firstWhere(test, orElse: orElse);
}
@override
M fold<M>(M initialValue, M Function(M previousValue, T element) combine) {
return _value.fold(initialValue, combine);
}
@override
Iterable<T> followedBy(Iterable<T> other) {
return _value.followedBy(other);
}
@override
void forEach(void Function(T element) action) {
return _value.forEach(action);
}
@override
Iterable<T> getRange(int start, int end) {
return _value.getRange(start, end);
}
@override
int indexOf(T element, [int start = 0]) {
return _value.indexOf(element, start);
}
@override
int indexWhere(bool Function(T element) test, [int start = 0]) {
return _value.indexWhere(test, start);
}
@override
bool get isEmpty => _value.isEmpty;
@override
bool get isNotEmpty => _value.isNotEmpty;
@override
Iterator<T> get iterator => _value.iterator;
@override
String join([String separator = ""]) {
return _value.join(separator);
}
@override
int lastIndexOf(T element, [int? start]) {
return _value.lastIndexOf(element, start);
}
@override
int lastIndexWhere(bool Function(T element) test, [int? start]) {
return lastIndexWhere(test, start);
}
@override
T lastWhere(bool Function(T element) test, {T Function()? orElse}) {
return _value.lastWhere(test, orElse: orElse);
}
@override
Iterable<M> map<M>(M Function(T e) toElement) {
return _value.map(toElement);
}
@override
T reduce(T Function(T value, T element) combine) {
return _value.reduce(combine);
}
@override
T removeLast() {
var val = _value.removeLast();
notifyListeners();
return val;
}
@override
void removeRange(int start, int end) {
_value.removeRange(start, end);
notifyListeners();
}
@override
void removeWhere(bool Function(T element) test) {
_value.removeWhere(test);
notifyListeners();
}
@override
void replaceRange(int start, int end, Iterable<T> replacements) {
_value.replaceRange(start, end, replacements);
notifyListeners();
}
@override
void retainWhere(bool Function(T element) test) {
_value.retainWhere(test);
notifyListeners();
}
@override
Iterable<T> get reversed => _value.reversed;
@override
void setAll(int index, Iterable<T> iterable) {
_value.setAll(index, iterable);
notifyListeners();
}
@override
void setRange(int start, int end, Iterable<T> iterable, [int skipCount = 0]) {
_value.setRange(start, end, iterable, skipCount);
notifyListeners();
}
@override
void shuffle([Random? random]) {
_value.shuffle(random);
notifyListeners();
}
@override
T singleWhere(bool Function(T element) test, {T Function()? orElse}) {
return _value.singleWhere(test, orElse: orElse);
}
@override
Iterable<T> skip(int count) {
return _value.skip(count);
}
@override
Iterable<T> skipWhile(bool Function(T value) test) {
return _value.skipWhile(test);
}
@override
void sort([int Function(T a, T b)? compare]) {
_value.sort(compare);
notifyListeners();
}
@override
List<T> sublist(int start, [int? end]) {
return _value.sublist(start, end);
}
@override
Iterable<T> take(int count) {
return _value.take(count);
}
@override
Iterable<T> takeWhile(bool Function(T value) test) {
return _value.takeWhile(test);
}
@override
List<T> toList({bool growable = true}) {
return _value.toList();
}
@override
Set<T> toSet() {
return _value.toSet();
}
@override
Iterable<T> where(bool Function(T element) test) {
return _value.where(test);
}
@override
Iterable<T> whereType<T>() {
return _value.whereType();
}
@override
set first(T value) {
_value.first = value;
notifyListeners();
}
@override
T get first => _value.first;
@override
set last(T value) {
_value.last = value;
notifyListeners();
}
@override
T get last => _value.last;
@override
set length(int newLength) {
_value.length = newLength;
notifyListeners();
}
@override
int get length => _value.length;
@override
T get single => _value.single;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment