Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active September 7, 2023 19:21
Show Gist options
  • Save slightfoot/2c12b9f87254b09664e4e9e1579b5f90 to your computer and use it in GitHub Desktop.
Save slightfoot/2c12b9f87254b09664e4e9e1579b5f90 to your computer and use it in GitHub Desktop.
List Notifier - 07/10/2020
// MIT License
//
// Copyright (c) 2020 Simon Lightfoot & Scott Stoll
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'dart:collection';
import 'package:flutter/foundation.dart' show ChangeNotifier, ValueListenable;
class ListNotifier<T> extends ChangeNotifier with ListMixin<T> implements ValueListenable<List<T>> {
ListNotifier([List<T> data]) : _data = data ?? <T>[];
final List<T> _data;
@override
List<T> get value => UnmodifiableListView<T>(_data);
@override
int get length => _data.length;
@override
set length(int value) {
_data.length = value;
notifyListeners();
}
@override
T operator [](int index) => _data[index];
@override
void operator []=(int index, T value) {
_data[index] = value;
notifyListeners();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment