Viewing version 0.12.0. Open the latest package page.

riverpod

0.12.0

A simple way to access state from anywhere in your application while robust and testable.

Download archive

Readme

pub package pub package pub package Build Status codecov Discord Shield

Welcome to Riverpod!

This project can be considered as a rewrite of provider to make improvements that would be otherwise impossible.

For learning how to use Riverpod, see its documentation: https://riverpod.dev

Also, don't forget to checkout our [Discord] channel, join us https://discord.gg/Bbumvej

Long story short:

  • Declare your providers as global variables:

    final myNotifierProvider = ChangeNotifierProvider((_) {
      return MyNotifier();
    });
    
    class MyNotifier extends ChangeNotifier {
      int count;
      // TODO: typical ChangeNotifier logic
    }
    
  • Use them inside your widgets in a compile-time safe way. No runtime exceptions!

    class Example extends ConsumerWidget {
      @override
      Widget build(BuildContext context, ScopedReader watch) {
        final count = watch(myNotifierProvider);
        return Text(count.toString());
      }
    }
    

See the FAQ if you have questions around what this means for provider.

Index

Motivation

If provider is a simplification of InheritedWidgets, then Riverpod is a reimplementation of InheritedWidgets from scratch.

It is very similar to provider in principle, but also has major differences as an attempt to fix the common problems that provider face.

Riverpod has multiple goals. First, it inherits the goals of provider:

  • Being able to safely create, observe and dispose states without having to worry about losing the state on widget rebuild.
  • Making our objects visible in Flutter's devtool by default.
  • Testable and composable
  • Improve the readability of InheritedWidgets when we have multiple of them (which would naturally lead to a deeply nested widget tree).
  • Make apps more scalable with a unidirectional data-flow.

From there, Riverpod goes a few steps beyond:

  • Reading objects is now compile-safe. No more runtime exception.
  • It makes the provider pattern more flexible, which allows supporting commonly requested features like:
    • Being able to have multiple providers of the same type.
    • Disposing the state of a provider when it is no longer used.
    • Have computed states
    • Making a provider private.
  • Simplifies complex object graphs. It is easier to depend on asynchronous state.
  • Makes the pattern independent from Flutter

These are achieved by no longer using InheritedWidgets. Instead, Riverpod implements its own mechanism that works in a similar fashion.

For learning how to use Riverpod, see its documentation: https://riverpod.dev

Contributing

Contributions are welcomed!

Here is a curated list of how you can help:

  • Report bugs and scenarios that are difficult to implement
  • Report parts of the documentation that are unclear
  • Update the documentation / add examples
  • Implement new features by making a pull-request

FAQ

Why another project when provider already exists?

While provider is largely used and well accepted by the community, it is not perfect either.

People regularly file issues or ask questions about some problems they face, such as:

  • Why do I have a ProviderNotFoundException?
  • How can I make that my state automatically disposed of when not used anymore?
  • How to make a provider that depends on other (potentially complex) providers?

These are legitimate problems, and I believe that something can be improved to fix those.

The issue is, these problems are deeply rooted in how provider works, and fixing those problems is likely impossible without drastic changes to the mechanism of provider.

In a way, if provider is a candle then Riverpod is a lightbulb. They have very similar usages, but we cannot create a lightbulb by improving our candle.

Is it safe to use in production?

Yes, but with caution.

Riverpod recently left its experimental status, but it isn't fully stable either. The API may change slightly when more features are added, and some use-cases may not be as simple as they could be.

But overall, you should be able to use Riverpod without trouble.

Will this get merged with provider at some point?

No. At least not until it is proven that the community likes Riverpod and that it doesn't cause more problems than it solves.

While provider and this project have a lot in common, they do have some major differences. Differences big enough that it would be a large breaking change for users of provider to migrate Riverpod.

Considering that, separating both projects initially sounds like a better compromise.

Will provider be deprecated/stop being supported?

Not in the short term, no.

This project is still experimental and unpopular. While it is, in a way, a provider 2.0, its worth has yet to be proven.

Until it is certain that Riverpod is a better way of doing things and that the community likes it, provider will still be maintained.

Changelog

0.12.0

0.11.2

0.11.0

  • package:riverpod/riverpod.dart now exports StateNotifier
  • Marked the providers with @sealed so that the IDE warns against implementing/subclassing providers.
  • Fix mistakes in AsyncValue.guard's documentation (thanks @mono0926)
  • Loosened the version constraints of freezed_annotation to support 0.12.0

0.10.0

  • Fixed a bug where the state of a provider may be disposed when it shouldn't be disposed.

  • Added a way to import the implementation class of providers with modifiers, such as AutoDisposeProvider.

    This is useful if you want to use Riverpod with the lint always_specify_types:

    import 'package:riverpod/all.dart';
    
    final AutoDisposeStateProvider<int> counter = StateProvider.autoDispose<int>((ProviderReference ref) {
      return 0;
    });
    

    If you do not use this lint, prefer using the default import instead, to not pollute your auto-complete.

0.8.0

0.7.0

0.6.1

  • Fixed a bug where when disposing ProviderContainer, providers may be disposed in an incorrect order.
  • Improved the performances of reading providers by 25%

0.6.0

  • Merged Computed and Provider. Now, all providers have the ability to rebuild their state when one of the object they listen changed.

    To migrate, change:

    final provider = Provider(...);
    final example = Computed((watch) {
      final value = watch(provider);
      return value;
    });
    

    into:

    final provider = Provider(...);
    final example = Provider((ref) {
      final value = ref.watch(provider);
      return value;
    });
    
  • Computed (now Provider) no-longer deeply compare collections to avoid rebuilds. Comparing the content of lists is quite expensive and actually rarely useful. Now, a simple == comparison is used.

  • Renamed ProviderStateOwner to ProviderContainer

  • Renamed ProviderStateOwnerObserver to ProviderObserver

  • It is no-longer possible to override a provider anywhere in the widget tree. Providers can only be overriden in the top-most ProviderContainer.

  • Providers can now read values which may change over time using ref.read and ref.watch. When using ref.watch, if the value obtained changes, this will cause the provider to re-create its state.

  • It is no-longer possible to add ProviderObserver anywhere in the widget tree. They can be added only on the top-most ProviderContainer.

  • Added ProviderContainer.refresh(provider). This method allows forcing the refresh of a provider, which can be useful for things like "retry on error" or "pull to refresh".

  • ref.read(StreamProvider<T>) no-longer returns a Stream<T> but an AsyncValue<T> Before:

    final streamProvider = StreamProvider<T>(...);
    final example = Provider((ref) {
      Stream<T> stream = ref.read(streamProvider);
    });
    

    After:

    final streamProvider = StreamProvider<T>(...);
    final example = Provider((ref) {
      Stream<T> stream = ref.watch(streamProvider.steam);
    });
    
  • ref.read(FutureProvider<T>) no-longer returns a Future<T> but an AsyncValue<T>

    Before:

    final futureProvider = FutureProvider<T>(...);
    final example = Provider((ref) {
      Future<T> future = ref.read(futureProvider);
    });
    

    After:

    final futureProvider = FutureProvider<T>(...);
    final example = Provider((ref) {
      Future<T> future = ref.watch(futureProvider.future);
    });
    
  • Removed ref.dependOn. You can now use ref.read/ref.watch to acheive the same effect.

    Before:

    final streamProvider = StreamProvider<T>(...);
    final example = Provider((ref) {
      Future<T> last = ref.dependOn(streamProvider).last;
    });
    

    After:

    final streamProvider = StreamProvider<T>(...);
    final example = Provider((ref) {
      Future<T> last = ref.watch(streamProvider.last);
    });
    
  • Provider.readOwner(ProviderStateOwner) is changed into ProviderContainer.read(Provider)

  • Provider.watchOwner(ProviderStateOwner, (value) {}) is changed into:

    ProviderContainer container;
    final provider = Provider((ref) => 0);
    
    final subscription = container.listen(
      provider,
      mayHaveChanged: (sub) {},
      didChange: (sub) {}.
    );
    
    subscription.close();
    
  • MyProvider.family.autoDispose now correctly free both the arguments and the associated providers from memory when the provider is no-longer listened.

  • Added ScopedProvider, a new kind of provider that can be overriden anywhere in the widget tree. Normal providers cannot read a ScopedProvider.

0.5.1

  • Fixed the documentation of StateNotifierProvider incorrectly showing the documentation of StreamProvider.
  • Improve the documentation of StateProvider.

0.5.0

0.4.0

Changed the syntax of "AutoDispose*" and "*Family" to use a syntax similar to named constructors instead.

Before:

final myProvider = AutoDisposeStateNotifierProviderFamily<MyStateNotifier, int>((ref, id) {
  return MyStateNotifier(id: id);
});

After:

final myProvider = StateNotifierProvider.autoDispose.family<MyStateNotifier, int>((ref, id) {
  return MyStateNotifier(id: id);
});

The behavior is the same. Only the syntax changed.

0.3.0

  • Added AsyncValue.whenData, syntax sugar for AsyncValue.when to handle only the data case and do nothing for the error/loading cases.

  • Fixed a bug that caused [Computed] to crash if it stopped being listened then was listened again.

0.2.1

  • Computed now correctly unsubscribe to a provider when their function stops using a provider.

0.2.0

  • ref.read is renamed as ref.dependOn
  • Deprecated ref.dependOn(streamProvider).stream and ref.dependOn(futureProvider).future in favor of a universal ref.dependOn(provider).value.
  • added ref.read(provider), syntax sugar for ref.dependOn(provider).value.

0.1.0

Initial release

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'riverpod:0.12.0'
pubspec.yaml
name: riverpod
description: >
  A simple way to access state from anywhere in your application
  while robust and testable.
version: 0.12.0
homepage: https://riverpod.dev
repository: https://github.com/rrousselGit/river_pod

environment:
  sdk: ">=2.8.0 <3.0.0"

dependencies:
  collection: ^1.14.12
  freezed_annotation: '>=0.11.0 <1.0.0'
  meta: ^1.1.8
  state_notifier: ">= 0.5.0 <1.0.0"

dev_dependencies:
  build_runner: ^1.8.1
  freezed: ^0.12.1
  mockito: ^4.1.0
  test: ^1.14.2
  test_coverage: ^0.4.1
  trotter: ^1.2.0

Version history

VersionPublishedArchiveSHA-256
0.0.1-preCached · Upstream SHA-256 matchedDownload4293ac1d…c5e806
0.0.1-pre+1Cached · Upstream SHA-256 matchedDownload303e5601…245d0a
0.1.0Cached · Upstream SHA-256 matchedDownload1a93b2a5…4a2699
0.2.0Cached · Upstream SHA-256 matchedDownload1e67d29d…38ae96
0.2.1Cached · Upstream SHA-256 matchedDownload35fc5b2f…6ad2c9
0.3.0Cached · Upstream SHA-256 matchedDownload21ed42e4…e54061
0.4.0Cached · Upstream SHA-256 matchedDownload922009d6…170583
0.5.0Cached · Upstream SHA-256 matchedDownloadb0c51f47…02b3b8
0.5.1Cached · Upstream SHA-256 matchedDownloada424df1b…399bb7
0.6.0-devCached · Upstream SHA-256 matchedDownloadce575b60…4f3294
0.6.0-dev+1Cached · Upstream SHA-256 matchedDownload7fad2e5f…a1059a
0.6.0-dev+2Cached · Upstream SHA-256 matchedDownloadfdd66c35…22e382
0.6.0-dev+3Cached · Upstream SHA-256 matchedDownload54f00828…a554cb
0.6.0Cached · Upstream SHA-256 matchedDownload8d04f41d…8f265d
0.6.0+1Cached · Upstream SHA-256 matchedDownload8e29da64…0a519f
0.6.1Cached · Upstream SHA-256 matchedDownload62162ffb…4e61cd
0.7.0Cached · Upstream SHA-256 matchedDownloadfb11b1c7…51bec2
0.8.0Cached · Upstream SHA-256 matchedDownloadc727b646…1352ce
0.10.0Cached · Upstream SHA-256 matchedDownload9ab76912…948d47
0.11.0Cached · Upstream SHA-256 matchedDownloadf0a45611…152874
0.11.2Cached · Upstream SHA-256 matchedDownload6adcf330…a03e28
0.12.0Cached · Upstream SHA-256 matchedDownload57e82963…e55b92
0.12.1Cached · Upstream SHA-256 matchedDownload38473ef0…09c05c
0.12.2Cached · Upstream SHA-256 matchedDownloadde0cb4aa…5c2d70
0.12.3Cached · Upstream SHA-256 matchedDownload83e9a709…deceb5
0.12.4Cached · Upstream SHA-256 matchedDownload69e07407…33e85c
0.13.0-nullsafety.0Cached · Upstream SHA-256 matchedDownloadf2464614…088a95
0.13.0-nullsafety.1Cached · Upstream SHA-256 matchedDownload1aff218f…d34efa
0.13.0-nullsafety.3Cached · Upstream SHA-256 matchedDownloadacda31a7…f13e6e
0.13.0Cached · Upstream SHA-256 matchedDownload05f0e1bd…1c5cc2
0.13.1Cached · Upstream SHA-256 matchedDownloadca4ef57a…3c79c6
0.14.0Cached · Upstream SHA-256 matchedDownload982e5733…803c17
0.14.0+1Cached · Upstream SHA-256 matchedDownloadaf9d98fb…1857e7
0.14.0+3Cached · Upstream SHA-256 matchedDownload13cbe0e1…4eae07
1.0.0-dev.0Cached · Upstream SHA-256 matchedDownload9c9b6700…e4217b
1.0.0-dev.1Cached · Upstream SHA-256 matchedDownloadda5b2bc2…a35c8a
1.0.0-dev.2Cached · Upstream SHA-256 matchedDownload70455873…857521
1.0.0-dev.3Cached · Upstream SHA-256 matchedDownload5a25043f…6ba9fe
1.0.0-dev.5Cached · Upstream SHA-256 matchedDownload919e02ea…a060af
1.0.0-dev.6Cached · Upstream SHA-256 matchedDownloadb663ec2f…6630f8
1.0.0-dev.7Cached · Upstream SHA-256 matchedDownload0f4a606a…cfa4b3
1.0.0-dev.8Cached · Upstream SHA-256 matchedDownload7bbbd1cc…a1a829
1.0.0-dev.9Cached · Upstream SHA-256 matchedDownload75a6d8e3…f72d13
1.0.0-dev.10Cached · Upstream SHA-256 matchedDownload38d86325…a12cc2
1.0.0Cached · Upstream SHA-256 matchedDownload8538f67c…37c771
1.0.1Cached · Upstream SHA-256 matchedDownloadea94268d…35fd13
1.0.2Cached · Upstream SHA-256 matchedDownloadcfd3d6bf…85cb17
1.0.3Cached · Upstream SHA-256 matchedDownloade7f09715…28217d
1.0.3+1Cached · Upstream SHA-256 matchedDownload98f9ac54…3e4485
2.0.0-dev.0Cached · Upstream SHA-256 matchedDownloadcf188b1a…31d37e
2.0.0-dev.1Cached · Upstream SHA-256 matchedDownload0de700b4…d64431
2.0.0-dev.2Cached · Upstream SHA-256 matchedDownload16b8fee4…afd455
2.0.0-dev.3Cached · Upstream SHA-256 matchedDownloadb4fec8df…c12d47
2.0.0-dev.4Cached · Upstream SHA-256 matchedDownload190ecb07…a32925
2.0.0-dev.5Cached · Upstream SHA-256 matchedDownload90de2f33…329efe
2.0.0-dev.6Cached · Upstream SHA-256 matchedDownload04716177…2e056f
2.0.0-dev.7Cached · Upstream SHA-256 matchedDownload2eae8f07…f5973d
2.0.0-dev.8Cached · Upstream SHA-256 matchedDownloadd4e6ac33…31c2c6
2.0.0-dev.9Cached · Upstream SHA-256 matchedDownloadccf59580…81ab2c
2.0.0Cached · Upstream SHA-256 matchedDownload427d64b8…e524a6
2.0.1Cached · Upstream SHA-256 matchedDownloadd52ac0cc…09cc21
2.0.2Cached · Upstream SHA-256 matchedDownload45b63637…cd2bac
2.1.0Cached · Upstream SHA-256 matchedDownloadce7f4a28…866156
2.1.1Cached · Upstream SHA-256 matchedDownload899cd099…1b29f9
2.1.2Cached · Upstream SHA-256 matchedDownload74bb148e…5d5be8
2.1.3Cached · Upstream SHA-256 matchedDownload0f43c64f…56e2c4
2.2.0Cached · Upstream SHA-256 matchedDownload59a48de9…9e8c4b
2.3.0Cached · Upstream SHA-256 matchedDownloadc5aea6c3…76cd25
2.3.1Cached · Upstream SHA-256 matchedDownload7da5a0fe…85bfa5
2.3.2Cached · Upstream SHA-256 matchedDownloadb0fbf792…cd8dc4
2.3.3Cached · Upstream SHA-256 matchedDownloadfc63714d…14b585
2.3.4Cached · Upstream SHA-256 matchedDownload77ab3bcd…df6ae5
2.3.5Cached · Upstream SHA-256 matchedDownloadec564106…8854b2
2.3.6Cached · Upstream SHA-256 matchedDownload80e48beb…e2d8ef
2.3.7Cached · Upstream SHA-256 matchedDownloadb0657b5b…ade793
2.3.9Cached · Upstream SHA-256 matchedDownload52b2937f…9a287c
2.3.10Cached · Upstream SHA-256 matchedDownload6c0a2c30…cb3f6a
2.4.0Cached · Upstream SHA-256 matchedDownloada600120d…f67d22
2.4.1Cached · Upstream SHA-256 matchedDownloadff676bd8…7cc914
2.4.3Cached · Upstream SHA-256 matchedDownload494bf2cf…f5e208
2.4.4Cached · Upstream SHA-256 matchedDownload2af3d127…d878b1
2.4.5Cached · Upstream SHA-256 matchedDownload2e843150…b63d55
2.4.6Cached · Upstream SHA-256 matchedDownload01fa385a…58d4b2
2.4.7Cached · Upstream SHA-256 matchedDownloadf003970b…840cf4
2.4.8Cached · Upstream SHA-256 matchedDownload08451ddb…478848
2.4.9Cached · Upstream SHA-256 matchedDownload942999ee…550b11
2.5.0Cached · Upstream SHA-256 matchedDownload548e2192…ba3589
2.5.1Cached · Upstream SHA-256 matchedDownloadf21b32ff…1dfa4d
2.5.3Cached · Upstream SHA-256 matchedDownloadc86fedfb…f7b5c9
2.6.0Cached · Upstream SHA-256 matchedDownloadbd6e656a…744c75
2.6.1Cached · Upstream SHA-256 matchedDownload59062512…a72959
3.0.0-dev.0Cached · Upstream SHA-256 matchedDownload41918a79…d2b723
3.0.0-dev.1Cached · Upstream SHA-256 matchedDownload2aaf880d…dcbc85
3.0.0-dev.2Cached · Upstream SHA-256 matchedDownload5bf1b037…48f915
3.0.0-dev.3Cached · Upstream SHA-256 matchedDownload0f41a697…44eaf2
3.0.0-dev.12Cached · Upstream SHA-256 matchedDownload114b10d3…b9700e
3.0.0-dev.13Cached · Upstream SHA-256 matchedDownload67aac6ba…429c52
3.0.0-dev.14Cached · Upstream SHA-256 matchedDownloaddaed1b04…173d63
3.0.0-dev.15Cached · Upstream SHA-256 matchedDownload4369c46c…2a37f9
3.0.0-dev.16Cached · Upstream SHA-256 matchedDownload0aa816ff…628f31
3.0.0-dev.17Cached · Upstream SHA-256 matchedDownload82507cfb…e91b8e
3.0.0-dev.18Cached · Upstream SHA-256 matchedDownloada64ca86e…2c85a6
3.0.0Cached · Upstream SHA-256 matchedDownload135723ec…be6d05
3.0.1Cached · Upstream SHA-256 matchedDownloadb21446f0…ce4810
3.0.2Cached · Upstream SHA-256 matchedDownloadf51e4fb8…81f564
3.0.3Cached · Upstream SHA-256 matchedDownloadc406de02…16aa59
3.1.0Cached · Upstream SHA-256 matchedDownload16ff608d…d4935a
3.2.0Cached · Upstream SHA-256 matchedDownload90266762…ec66f1
3.2.1Cached · Upstream SHA-256 matchedDownload8c22216b…279f83
3.3.2-dev.1Cached · Upstream SHA-256 matchedDownloadb3dd973b…d44461
3.3.2-dev.2Cached · Upstream SHA-256 matchedDownloadda723396…042b3f
3.3.2Cached · Upstream SHA-256 matchedDownload17100416…126c76
3.4.0Cached · Upstream SHA-256 matchedDownloadb4184653…48d786
3.4.1Cached · Upstream SHA-256 matchedDownloadccd1d45f…21b436
3.4.2Cached · Upstream SHA-256 matchedDownload84351b34…8e50f1
3.4.3Cached · Upstream SHA-256 matchedDownload484dfc87…ba4b7a

Dependencies

collection "^1.14.12"

freezed_annotation "\u003e=0.11.0 \u003c1.0.0"

meta "^1.1.8"

state_notifier "\u003e= 0.5.0 \u003c1.0.0"