All Projects → google → Flutter Provide

google / Flutter Provide

Licence: other
A simple framework for state management in Flutter.

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to Flutter Provide

Vue Stash
Easily share reactive data between your Vue components.
Stars: ✭ 412 (-50%)
Mutual labels:  state-management
Konfig
Composable, observable and performant config handling for Go for the distributed processing era
Stars: ✭ 597 (-27.55%)
Mutual labels:  state-management
Vuex
🗃️ Centralized State Management for Vue.js.
Stars: ✭ 27,115 (+3190.66%)
Mutual labels:  state-management
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+449.15%)
Mutual labels:  state-management
Cubit
Cubit is a lightweight state management solution. It is a subset of the bloc package that does not rely on events and instead uses methods to emit new states.
Stars: ✭ 539 (-34.59%)
Mutual labels:  state-management
Focal
Program user interfaces the FRP way.
Stars: ✭ 613 (-25.61%)
Mutual labels:  state-management
Dutier
The immutable, async and hybrid state management solution for Javascript applications.
Stars: ✭ 401 (-51.33%)
Mutual labels:  state-management
React Final Form
🏁 High performance subscription-based form state management for React
Stars: ✭ 6,781 (+722.94%)
Mutual labels:  state-management
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (-31.19%)
Mutual labels:  state-management
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (-17.11%)
Mutual labels:  state-management
Roxie
Lightweight Android library for building reactive apps.
Stars: ✭ 441 (-46.48%)
Mutual labels:  state-management
React Sweet State
Shared state management solution for React
Stars: ✭ 537 (-34.83%)
Mutual labels:  state-management
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (-20.63%)
Mutual labels:  state-management
Jumpstate
Jumpstate is a simple and powerful state management utility for Redux.
Stars: ✭ 429 (-47.94%)
Mutual labels:  state-management
Mobx State Tree
Full-featured reactive state management without the boilerplate
Stars: ✭ 6,317 (+666.63%)
Mutual labels:  state-management
React Recollect
State management for React
Stars: ✭ 411 (-50.12%)
Mutual labels:  state-management
React Hooks Global State
Simple global state for React with Hooks API without Context API
Stars: ✭ 605 (-26.58%)
Mutual labels:  state-management
Hox
The next-generation state manager for React.
Stars: ✭ 815 (-1.09%)
Mutual labels:  state-management
Spruce
A lightweight state management layer for Alpine.js. 🌲
Stars: ✭ 720 (-12.62%)
Mutual labels:  state-management
Rcre
Build complex applications without pain
Stars: ✭ 684 (-16.99%)
Mutual labels:  state-management

DEPRECATION NOTE: After discussion in the Flutter community over the difference between this package, package:provider, and package:scoped_model (all with a similar, InheritedWidget-based philosophy), we have decided to merge these efforts.

The community-initiated provider supersedes this package, provide. Get provider here.

To be clear, this package will always be available at pub.dev/packages/provide — pub doesn't allow packages to "disappear". But the package likely won't be upgraded.

See Issue #3 for the full discussion.


This package contains classes to allow the passing of data down the widget tree. It is designed as a replacement for ScopedModel that allows for more flexible handling of data types and data.

Key widgets and static methods

  • Provide<T> - Widget used to obtain values from a ProviderNode higher up in the widget tree and rebuild on change. The Provide<T> widget should only be used with Streams or Listenables. Equivalent to ScopedModelDescendant in ScopedModel.

  • Provide.value<T> - Static method used to get a value from a ProviderNode using the BuildContext. This will not rebuild on change. Similar to manually writing a static .of() method for an InheritedWidget.

  • Provide.stream<T> - Static method used to get a Stream from a ProviderNode. Only works if either T is listenable, or if the Provider comes from a Stream.

  • Provider<T> - A class that returns a typed value on demand. Stored in a ProviderNode to allow retrieval using Provide.

  • ProviderNode - The equivalent of the ScopedModel widget. Contains Providers which can be found as an InheritedWidget.

Usage

This is a simple example of a counter app:

/// A provide widget can rebuild on changes to any class that implements
/// the listenable interface.
///
/// Here, we mixin ChangeNotifier so we don't need to manage listeners
/// ourselves.
///
/// Extending ValueNotifier<int> would be another simple way to do this.
class Counter with ChangeNotifier {
  int _value;

  int get value => _value;

  Counter(this._value);

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

/// CounterApp which obtains a counter from the widget tree and uses it.
class CounterApp extends StatelessWidget {
  // The widgets here get the value of Counter in three different
  // ways.
  //
  // - Provide<Counter> creates a widget that rebuilds on change
  // - Provide.value<Counter> obtains the value directly
  // - Provide.stream<Counter> returns a stream
  @override
  Widget build(BuildContext context) {
    // Gets the Counter from the nearest ProviderNode that contains a Counter.
    // This does not cause this widget to rebuild when the counter changes.
    final currentCounter = Provide.value<Counter>(context);

    return Column(children: [
      // Simplest way to retrieve the provided value.
      //
      // Each time the counter changes, this will get rebuilt. This widget
      // requires the value to be a Listenable or a Stream. Otherwise
      Provide<Counter>(
        builder: (context, child, counter) => Text('${counter.value}'),
      ),

      // This widget gets the counter as a stream of changes.
      // The stream is filtered so that this only rebuilds on even numbers.
      StreamBuilder<Counter>(
          initialData: currentCounter,
          stream: Provide.stream<Counter>(context)
              .where((counter) => counter.value % 2 == 0),
          builder: (context, snapshot) =>
              Text('Last even value: ${snapshot.data.value}')),

      // This button just needs to call a method on Counter. No need to rebuild
      // it as the value of Counter changes. Therefore, we can use the value of
      // `Provide.value<Counter>` from above.
      FlatButton(child: Text('increment'), onPressed: currentCounter.increment),

      Text('Another widget that does not depend on the Counter'),
    ]);
  }
}

void main() {
    // The class that contains all the providers. This shouldn't change after
    // being used.
    //
    // In this case, the Counter gets instantiated the first time someone uses
    // it, and lives as a singleton after that.
    final providers = Providers()
      ..provide(Provider.function((context) => Counter(0)));

    runApp(ProviderNode(
      providers: providers,
      child: CounterApp(),
    ));
}

How it works

Similar to ScopedModel, this relies on InheritedWidgets in order to propagate data up and down the widget tree. However, unlike ScopedModel, rather than storing a single concrete type, a ProviderNode contains a map of Types to Providers. This means that a single node can contain any number of providers, and that a provider of a type doesn't have to be of the exact concrete type.

Somewhere in the tree, there is a ProviderNode, which contains a set of Providers. When a Provide widget is created, it searches up the widget tree for a ProviderNode that contains a provider for its requested type. It then listens for any changes to that requested type.

There are also static methods that operate on BuildContext that allow any widget's build function to get data from ProviderNodes without listening to changes directly.

Useful widgets to use with Provider

  • ChangeNotifier — Easy way to implement Listenable. The equivalent of Model from ScopedModel.

  • ValueNotifier — Wrapping your mutable state in ValueNotifier<T> can save you from missing notifyListener calls.

  • StreamBuilder — Can be used with Provide.stream to have widgets that rebuild on stream changes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].