Market LiveNIFTY25,259-0.16%SENSEX82,082.4+0.21%Gold 24K ₹12,098.38/g down302.9Silver ₹146.46/g down5.43
InsightforGeeks
Flutter

Flutter State Management for Beginners: setState, Provider and Riverpod Compared

insightforgeeks-editorial
17 Nov 2026
4 min read
Share:

In Flutter, state is any data that can change while the app runs: a counter, a form field, a logged-in user, a list from a server. When state changes, the screen must update. How you hold and share that state is called state management. Here are three common choices and when to use each.

1. setState: the simplest option

Use setState inside a StatefulWidget when the data belongs to one widget only.

class CounterPage extends StatefulWidget {
  const CounterPage({super.key});
  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => setState(() => count++),
      child: Text('Clicked $count times'),
    );
  }
}

It is easy to learn, but once several screens need the same data you end up passing values through many constructors.

2. Provider: share state down the tree

The provider package places a state object above your widgets so that any widget below can read it.

class CartModel extends ChangeNotifier {
  final List<String> items = [];
  void add(String item) {
    items.add(item);
    notifyListeners();
  }
}

// In main():
runApp(ChangeNotifierProvider(
  create: (_) => CartModel(),
  child: const MyApp(),
));

// In a widget:
final cart = context.watch<CartModel>();

context.watch rebuilds the widget when notifyListeners() is called. context.read reads without listening, which suits button handlers.

3. Riverpod: Provider's successor

Riverpod, by the same author as Provider, removes the dependency on the widget tree. It is safer to test, catches more mistakes at compile time and handles asynchronous data such as API calls neatly.

final counterProvider = StateProvider<int>((ref) => 0);

class CounterButton extends ConsumerWidget {
  const CounterButton({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final count = ref.watch(counterProvider);
    return ElevatedButton(
      onPressed: () => ref.read(counterProvider.notifier).state++,
      child: Text('$count'),
    );
  }
}

Which should you choose?

  • One widget, local data: setState.
  • Small to medium app, shared data: Provider is enough and widely documented.
  • Larger app, async data, lots of testing: Riverpod.

Other solid options include Bloc and Cubit. The best choice is the one your team understands. Start simple and move to a bigger tool only when the code gets hard to follow. Check the package pages on pub.dev for current versions and syntax, because these APIs change between releases.

Lifting state up

Before choosing a package, understand the basic rule: when two widgets need the same data, move the state to their closest common parent and pass it down. Many apps never need more than this, a few StatefulWidgets and callbacks, until the tree gets deep. The signs that you have outgrown it are long chains of constructor parameters that only pass data through, and widgets that rebuild far more than they should.

Rebuilds and performance

  • Keep state as low in the widget tree as possible so that fewer widgets rebuild.
  • Mark widgets const where you can, because Flutter can skip rebuilding them.
  • With Provider, use context.select to listen to one field instead of the whole model.
  • With Riverpod, use ref.watch(provider.select(...)) for the same purpose.
  • Use the "Track widget rebuilds" option in Flutter DevTools to see what rebuilds and why.

Handling async data

Most real screens load data from an API, so you need loading, success and error states. Riverpod's FutureProvider gives you this directly:

final userProvider = FutureProvider<User>((ref) async {
  final res = await http.get(Uri.parse('https://example.com/api/me'));
  return User.fromJson(jsonDecode(res.body));
});

// In a ConsumerWidget:
final user = ref.watch(userProvider);
return user.when(
  data: (u) => Text(u.name),
  loading: () => const CircularProgressIndicator(),
  error: (e, _) => Text('Something went wrong'),
);

Where Bloc and Cubit fit

Bloc separates events from states and is popular in larger teams because it forces a clear structure and is easy to test. A Cubit is a simpler version that exposes methods instead of events. If your team already knows Bloc, there is no need to switch to Riverpod; the best solution is the one the team uses consistently.

Testing state

One advantage of keeping logic out of widgets is that you can test it as plain Dart. A ChangeNotifier or a Riverpod notifier can be created in a test, called, and checked without drawing any screen. Widget tests can then cover the user interface separately.

A practical path for beginners

  1. Build your first screens with setState only.
  2. When two screens share data, try Provider or Riverpod for that data alone.
  3. Keep business logic in separate classes from widgets.
  4. Read the official Flutter "State management" guide and each package's documentation for the current recommended style.
Tags:#flutter#state management#dart
Found this helpful? Share it with friends & colleagues
Share:
insightforgeeks-editorial Verified Editorial Staff

Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.

Published on 17 November 2026General Information · Disclaimer

Recommended based on your reading interest

More in Flutter