Module 1
Topic 6

StateProvider

Managing simple mutable state in Riverpod — the easiest way to handle counters, toggles, and form inputs.

Riverpod – StateProvider Guide
What Is StateProvider?

StateProvider is a type of provider in Riverpod that holds a mutable piece of state . It's the simplest way to manage state that changes over time, such as:

  • Counter values
  • Toggle states (boolean)
  • Text input values
  • Selected item indices
  • Simple form data

✅ When to Use StateProvider

Use StateProvider when you need to manage simple, primitive state that doesn't have complex validation or business logic. It's perfect for UI state like what tab is selected, whether a switch is on, or how many items are in a cart.

Defining a StateProvider

Defining a StateProvider is straightforward. You provide an initial value, and Riverpod handles the rest.

Reading and Updating StateProvider

To read the value of a StateProvider , use ref.watch() . To update it, use ref.read(provider.notifier).state = newValue .

Step-by-Step Explanation
1.
Define the Provider counterProvider is a StateProvider that holds an integer. The initial value is 0 .
2.
Watch the Provider ref.watch(counterProvider) listens to changes. When the counter changes, the widget rebuilds with the new value.
3.
Update the State ref.read(counterProvider.notifier).state++ increments the counter. The .notifier gives you access to the state object, and .state is where the actual value is stored.
4.
Rebuild – After the state is updated, Riverpod automatically notifies any widgets that are watching the provider, and they rebuild with the new value.
Complete Example: Counter App

Here's a complete, runnable counter app that uses StateProvider . This is the simplest possible Riverpod app.

StateProvider vs Other Providers

StateProvider

  • Simple mutable state
  • Primitive values (int, bool, String)
  • No business logic
  • Direct state mutation

StateNotifierProvider

  • Complex state with methods
  • Custom state classes
  • Business logic inside the notifier
  • Immutability recommended

FutureProvider

  • Asynchronous data (Future)
  • Loading and error states
  • One-time fetch
  • Read-only

StreamProvider

  • Real-time data (Stream)
  • Continuous updates
  • Loading and error states
  • Read-only

💡 When to Choose StateProvider

Choose StateProvider when: you have simple state that doesn't need validation, transformation, or complex business logic. It's perfect for UI state and simple data. Choose StateNotifierProvider when: you have complex state with methods, validation, or immutability requirements.

Common Mistakes
❌ Mistake 1: Directly assigning to state without .notifier

ref.read(counterProvider).state = 5 won't work. You must use .notifier .

✅ Correct: Use .notifier to update state

ref.read(counterProvider.notifier).state = 5 is the correct way.

❌ Mistake 2: Using ref.watch() inside event handlers

ref.watch() should only be used inside the build() method.

✅ Correct: Use ref.read() in event handlers

For buttons and callbacks, use ref.read() to update state without listening for changes.

❌ Mistake 3: Using StateProvider for complex state

If your state has methods, validation, or multiple fields, StateProvider becomes cumbersome.

✅ Correct: Use StateNotifierProvider for complex state

For complex state with methods and validation, use StateNotifierProvider .

🎯 Key Takeaway

StateProvider is the simplest way to manage mutable state in Riverpod. Use it for counters, toggles, and form inputs. For more complex state, use StateNotifierProvider . Remember: .notifier is the key to updating state!