Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.59 KB

README.md

File metadata and controls

49 lines (36 loc) · 1.59 KB

multi_listenable_builder

A widget builder whose content stays synced with one or more Listenables.

What is this?

Many of us flutter developers use ChangeNotifier for state management, they may use ListenableBuilder to keep their widgets in sync with the model, but it's a mess when they need to listen to more than one Listenables.

Example

Let's say you have a model of ChangeNotifier like this:

class CounterModel extends ChangeNotifier {
  int _count = 0;

  void increment() {
    _count++;
    notifyListeners();
  }

  int get count => _count;
}

Then in the build method of your widget:

MultiListenableBuilder(
  // supply the instance of `ChangeNotifier` model,
  // whether you get it from the build context or anywhere
  notifiers: [_counterModel],
  // this builder function will be executed,
  // once the `ChangeNotifier` model is updated
  builder: (BuildContext context, _) {
    return Text(
      '${_counterModel.count}',
      style: Theme.of(context).textTheme.headlineMedium,
    );
  },
)

Contributions

Feel free to contribute to this project.

If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue. If you fixed a bug or implemented a feature, please send a pull request.