Home > Blockchain >  FutureProvider vs StreamProvider vs ChangeNotifierProvider
FutureProvider vs StreamProvider vs ChangeNotifierProvider

Time:09-21

to be short and clear i am new to flutter and i would like to know the diffrence between FutureProvider and StreamProvider and ChangeNotifierProvider and when to use each one Thanks in advance

CodePudding user response:

FutureProvider

In a nutshell, FutureProvider is used to provide a value that might not be ready by the time the widget tree is ready to use it's values. Therefor, the main use-case of FutureProvider is to ensure that a null value isn't passed to any widgets. The future provider has an initial value, which widgets can use until the Future value is resolved. When resolved, it the FutureProvider will tell it's descendents to rebuild, using the new value.

Importantly, this means that the widgets who rely on the value of a future provider will only rebuild once. It will display the initial value, and then the provided future value, and then won't rebuild again.

Future provider can be configured to change again if there is, for some reason, another new value from the future. But, if you anticipate multiple values from the provider, you should likely be using a StreamProvider.

StreamProvider

StreamProvider provides, well, Streamed values. Like FutureProvider, provided values will be auto-magically passed the new values of the provided value as they come in. The major difference is that the values will trigger a re-build as many times as it needs to.

ChangeNotifierProvider

Most of the examples you'll see on the internets is using the ChangeNotifierProvider, and it's also the class you'll likely use most often. This class is basically a provider-wrapper over a class that implements ChangeNotifier.

According to the Flutter docs, a ChangeNotifier is 'a class that can be extended or mixed in that provides a change notification API using VoidCallback for notifications.' In practical terms, other objects can listen to a ChangeNotifier object. And, when the change notifier gets updated values, it can call a method called 'notifyListeners()', and then any of it's listeners will respond with an action. Listening to a change notifier is done by registering a callback, which is called when notifyListeners is invoked.

For Details Information with Example you can follow links FutureProvider StreamProvider ChangeNotifierProvider

  • Related