complex topic. If you feel that some of your questions haven’t been answered, or that the approach described on these pages is not viable for your use cases, you are probably right 10 Flutter State management
is declarative. This means that Flutter builds its user interface to reflect the current state of your app When the state of your app changes (for example, the user flips a switch in the settings screen), you change the state, and that triggers a redraw of the user interface. https://docs.flutter.dev/development/data-and-backend/state-mgmt/declarative
• User preferences • Login info • Notifications in a social networking app • The shopping cart in an e-commerce app • Read/unread state of articles in a news app • current page in a PageView • current progress of a complex animation • current selected tab in a BottomNavigationBar
https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple Widget build(BuildContext context) { var cartModel = somehowGetMyCartModel(context); return SomeWidget( // Just construct the UI once, using the current state of the cart. ); }
Base class for widgets that efficiently propagate information down the tree. To obtain the nearest instance of a particular type of inherited widget from a build context, use BuildContext.dependOnInheritedWidgetOfExactType. Inherited widgets, when referenced in this way, will cause the consumer to rebuild when the inherited widget itself changes state. https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html
rarely come with all the information necessary to render their User Interface. Instead, the data is often fetched asynchronously from a server. The problem is, working with asynchronous code is hard. Although Flutter comes with some way to store state, it doesn't do much besides that. Thus, a number of challenges remain unsolved: • Asynchronous requests need to be cached locally, as it would be unreasonable to re-execute them whenever the UI refreshes. • Since we have a cache, our cache could get out of date if we're not careful. • We also need to handle errors and loading states
objects is now compile-safe. No more runtime exception. • It makes the provider pattern more flexible, which allows supporting commonly requested features like: a. Being able to have multiple providers of the same type. b. Disposing the state of a provider when it is no longer used. c. Have computed states d. Making a provider private. • Simplifies complex object graphs. It is easier to depend on asynchronous state. • Makes the pattern independent from Flutter
Provider Type Provider Create Function Example Use Case Provider ( fully immutable.) Returns any type A service class / computed property (filtered list) StateProvider Returns any type A filter condition / simple state object FutureProvider Returns a Future of any type A result from an API call StreamProvider Returns a Stream of any type A stream of results from an API StateNotifierProvider Returns a subclass of StateNotifier A complex state object that is immutable except through an interface ChangeNotifierProvider Returns a subclass of ChangeNotifier A complex state object that requires mutability (Async)NotifierProvider - New 2.0
Provider Type Provider Create Function Example Use Case Provider Returns any type A service class / computed property (filtered list) StateProvider Returns any type A filter condition / simple state object FutureProvider Returns a Future of any type A result from an API call StreamProvider Returns a Stream of any type A stream of results from an API StateNotifierProvider Returns a subclass of StateNotifier A complex state object that is immutable except through an interface ChangeNotifierProvider Returns a subclass of ChangeNotifier A complex state object that requires mutability (Async)NotifierProvider - New 2.0
StateProvider exists primarily to allow the modification of simple variables by the User Interface. The state of a StateProvider is typically one of: • an enum, such as a filter type • a String, typically the raw content of a text field • a boolean, for checkboxes • a number, for pagination or age form fields You should not use StateProvider if: • your state needs validation logic • your state is a complex object (such as a custom class, a list/map, ...) • the logic for modifying your state is more advanced than a simple count++. https://docs-v2.riverpod.dev/docs/providers/state_provider
App ProviderScope MaterialApp ConsumerWidget Scaffold Column Text Consumer Fab Text Tab! ref.read(counterPr ovider.notifier).stat e++ sub ref.watch(counterProvider)
provider = Provider((ref) { // use ref to obtain other providers final repository = ref.watch(repositoryProvider); return SomeValue(repository); }) final counterProvider = StateNotifierProvider<Counter, int>((ref) { return Counter(ref); }); class Counter extends StateNotifier<int> { Counter(this.ref): super(0); final Ref ref; void increment() { // Counter can use the "ref" to read other providers final repository = ref.read(repositoryProvider); repository.post('...'); } }
NotifierProvider class TodosNotifier extends StateNotifier<List<Todo>> { TodosNotifier(): super([]); void addTodo(Todo todo) { state = [...state, todo]; } // Let's allow removing todos void removeTodo(String todoId) { state = [ for (final todo in state) if (todo.id != todoId) todo, ]; } }
to the changes of a ProviderContainer. • didAddProvider is called every time a provider was initialized, and the value exposed is value. • didDisposeProvider is called every time a provider was disposed. • didUpdateProvider is called every time by providers when they emit a notification.
be deprecated. But that's because Riverpod is it's successor, and I actively work on Riverpod For the "Provider will be deprecated", there are two things to keep in mind: - I will write a tool to automatically migrate from Provider to Riverpod - I'm currently working on a Flutter PR to allow `http:/ /context.watch` in Riverpod (although unclear whether it'll pass) @Remi Rousselet, 2022.08.14
Management a. Everything is a widget b. Widget = immutable c. State = Lifecycle 2. List of flutter state management a. setState() b. inheritedWidget c. Scope Model, BLoC, GetX, Provider, Riverpod…etc 3. Riverpod a. Hmmmm, Riverpod 2.0! b. Provider, StateProvider, FutureProvider, StreamProvider and NotifierProvider(new) c. Riverpod Graph(WIP), Generator, Lint!