Sub topic 1
Why `setState` Is Not Enough: Ephemeral State vs App State
Prerequisites
This chapter assumes Dart basics, null safety, classes, and async/await. You should also know Flutter’s StatelessWidget, StatefulWidget, BuildContext, and setState. Later examples assume basic familiarity with streams.
Flutter state is any value that can change while a widget is running. A counter value, selected tab, logged-in user, and loading status are all state. The important question is not whether state exists, but where it belongs and who needs to change it.
Ephemeral State
Ephemeral state belongs to one widget or one small part of the screen. A password field’s visibility, the current page of a local tab bar, and whether an animation is running are common examples.
A StatefulWidget can manage this state with setState:
import 'package:flutter/material.dart';
class CounterCard extends StatefulWidget {
const CounterCard({super.key});
@override
State<CounterCard> createState() => _CounterCardState();
}
class _CounterCardState extends State<CounterCard> {
int count = 0;
void increment() {
setState(() {
count++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Count: $count'),
ElevatedButton(
onPressed: increment,
child: const Text('Add'),
),
],
);
}
}Calling setState tells Flutter that this widget’s state changed. Flutter then calls build again for that widget subtree. The state stays private, and the widget directly controls its updates.
App State
App state is state that multiple widgets, screens, or services need to read or change. Examples include a shopping cart, an authenticated account, a list loaded from an API, and the current request status.
Passing this state through many constructors can create “prop drilling”: a value travels through widgets that do not use it simply to reach a deeper widget. Several widgets may also implement similar update rules, making behavior harder to test and keep consistent.
Bloc and Cubit provide a separate owner for app state. The owner exposes a current state and controlled operations that produce new states. Widgets display the state, while the Cubit or Bloc contains the decision-making logic.
| State kind | Typical owner | Example |
|---|---|---|
| Ephemeral | One widget | Password visibility |
| App-level | Cubit or Bloc | Cart contents |
Why Separation Matters
This separation gives each part a focused responsibility:
- The widget renders state and sends user intent.
- The Cubit or Bloc applies business rules.
- Tests can exercise state changes without building a Flutter screen.
- Multiple widgets can subscribe to the same state source.
setState is not wrong or obsolete. It becomes difficult when a widget must coordinate network calls, errors, caching, navigation decisions, and updates from several parts of the application. Moving those rules into a Cubit or Bloc makes the flow explicit. The next chapters show how to model that state and connect it to the widget tree.
Exercise
A search field has a local “show clear button” flag, while the search results are shared by the results page and a suggestion panel. Where should each value live?
Show answer
Keep the clear-button flag in the field’s local state. Put the shared search results in a Cubit or Bloc.