Skip to content

Implement change notifier for collection object e.g. List, Set and Map

License

Notifications You must be signed in to change notification settings

rk0cc/collection_change_notifier

Repository files navigation

Binding Dart collection model to ChangeNotifier in Flutter Flutter package test

Pub Version (including pre-releases)

This package implemented Dart's collection object with ChangeNotifier that it updated when collection item changed.

Also, online demo available

Purpose

Flutter provides ChangeNotifier for handling update when mutable object's property changed and make widget rebuild (provider package). However, when it handle multiple items under a single object, it required specific implementation by yourself which limited features from collections when required. As a reault, this package provides List, Map and Set with ChangeNotifier integration.

It similar how BindingList worked in .NET.

Install

Edit pubspec.yaml:

  • From pub.dev
dependencies:
    collection_change_notifier: ^1.0.0+1 # Or ''>=1.0.0 <1.1.0' if required same minor version
  • From Git (For unstable release)
dependencies:
    git:
        url: https://github.com/rk0cc/collection_change_notifier.git
        ref: (Commit hash, branches or tags name)

Usage

Mostly it integrated with notifyListener already when editing items in the collections unless changing state of element directly which required to uses modify to notify update:

class IntState {
    int state;

    IntState(this.state);
}

final ListChangeNotifier<IntState> lcnis = ListChangeNotifier()..add(IntState(1));

// Attach lcnis to ChangeNotifierProvider.value

// Do not change element state directly, this action will not trigger Flutter to rebuild context.
lcnis[0].state = 2;

// Call modify if want to trigger Flutter rebuild when element state changed via `modify`:
lcnis.modify(0, (item) {
    item.state = 2;
});

// For map:
final MapChangeNotifier<String, IntState> mscnis = MapChangeNotifier()..["one"] = IntState(1);

mscnis.modify("one", (item) {
    // Null check required to ensure it is assigned (when value type is non-nullable)
    if (item != null) {
        item.state = 2;
    }
    // item!.state = 2; // (Not recommeded which make more complicated for error hadling)
})

Limitations

It only implemented based on the class method which there is no ovridden method for extensions. Extensions method tend to be invoked multiple time of notifyListeners that causing build error throw.

License

BSD-3