Dart Tutorial

State Management

setState, Providers, and Riverpod in Flutter

What is State?

In Flutter, "State" is information that can change during the lifetime of a widget and that you want the UI to reflect. When the state changes, the UI must be rebuilt.

1. setState (Local State)

For simple widgets (like a checkbox or a counter), you can use the built-in setState method. It tells Flutter: "Something changed, please redraw this widget."
dart
setState(() {
  _counter++;
});

2. Lifting State Up

Wait! As your app grows, you'll need to share state between many different widgets. This is where Global State Management comes in.
SolutionBest For...
ProviderThe classic, standard way to share data.
RiverpodA modern, safer, and more powerful version of Provider.
BLoCLarge-scale apps using Streams and Events.
GetXHigh-speed, high-performance, and very simple syntax.

Why use a library?

  1. Efficiency: Only rebuild the specific widgets that need the new data.
  2. Separation: Keep your business logic (the "What") separate from your UI (the "How").
  3. Testability: Easier to write unit tests for your data logic without needing to run the whole app.

Summary

TermDescription
StateData that changes over time
RebuildRedrawing the UI to match the new state
ProviderA popular "Wrapper" for sharing data down the tree
ReactiveUI that "reacts" automatically to data changes
Best ForKeeping your app data organized and synchronized
Logic"One Source of Truth"
SafetyPrevents "Out of Sync" UI bugs
ModernRiverpod is currently the recommended choice for new apps
StandardState management is the #1 topic in Flutter architecture
IdentityThe "BLoC" pattern is unique to the Flutter/Dart ecosystem
ObservableData that can be "watched" for changes