Sub topic 10
Lifting State Up
Assume you know basic Dart syntax, null safety, and how to run a Flutter app. You should also know the difference between StatelessWidget, StatefulWidget, and setState.
A widget should own state when that state belongs only to the widget. When two widgets need the same changing value, move the value to their nearest common parent. This design is called lifting state up.
The parent becomes the single source of truth:
- The parent stores the current value.
- The parent passes data down through constructor parameters.
- A child reports user actions upward through a callback.
- The parent changes the value with
setState. - Flutter rebuilds the affected part of the tree.
Data down, events up
Suppose a screen contains a label and a favorite button. Both need to know whether an item is favorited. If the button stores its own Boolean, the label cannot reliably update. Store the Boolean in the screen instead.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: FavoriteScreen()));
}
class FavoriteScreen extends StatefulWidget {
const FavoriteScreen({super.key});
@override
State<FavoriteScreen> createState() => _FavoriteScreenState();
}
class _FavoriteScreenState extends State<FavoriteScreen> {
bool _isFavorite = false;
void _toggleFavorite() {
setState(() {
_isFavorite = !_isFavorite;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Favorite')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_isFavorite ? 'Saved' : 'Not saved'),
FavoriteButton(
isFavorite: _isFavorite,
onToggle: _toggleFavorite,
),
],
),
),
);
}
}
class FavoriteButton extends StatelessWidget {
const FavoriteButton({
super.key,
required this.isFavorite,
required this.onToggle,
});
final bool isFavorite;
final VoidCallback onToggle;
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: onToggle,
icon: Icon(
isFavorite ? Icons.favorite : Icons.favorite_border,
),
tooltip: isFavorite ? 'Remove favorite' : 'Add favorite',
);
}
}FavoriteButton does not decide whether the item is favorited. It receives the current value and displays it. Its onToggle callback communicates an event without knowing how the parent will handle it.
A callback is a function passed to another object. VoidCallback is Flutter’s type for a function that takes no arguments and returns nothing.
The ownership boundary
Use the lowest common ancestor that needs the state. If only one text field uses a value, keep it in that field. If a field and a preview use the value, their parent should own it. If many distant screens use it, a shared state-management solution may be more suitable than passing callbacks through many layers.
Common errors
A common mistake is changing a child’s local copy and expecting the parent to know about it. The child must call the callback instead.
Another mistake is passing an initial value to a stateful child and assuming the child will automatically track later changes. If the child copies that value into its own state, it must handle didUpdateWidget, or the state should remain in the parent.
Keep callbacks small and intentional. The child should report what happened. The parent should decide what that event means and update the shared state.
Exercise
A screen has a switch and a text label that must show the same setting. Where should the setting live?
Show answer
Store it in their nearest common parent, pass the Boolean to both widgets, and pass a callback to the switch so the parent changes it with setState.