Where should shared state be placed?
Shared state goes in the lowest common ancestor of all widgets that need it.
Share state between widgets properly
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
When two or more widgets need to read or modify the same piece of state, keep that state in their lowest common ancestor widget. The ancestor holds the data in its State class and passes it down to children through constructors, along with callback functions that children can call to request changes.
1// Before: State duplicated in each widget2// After: State lifted to parent34class ShoppingApp extends StatefulWidget {5@override6_ShoppingAppState createState() => _ShoppingAppState();7}89class _ShoppingAppState extends State<ShoppingApp> {10List<String> cartItems = []; // Lifted state1112void addToCart(String item) {13setState(() {14cartItems.add(item);15});16}1718@override19Widget build(BuildContext context) {20return Column(21children: [22// Pass state down to children23ProductList(onAddToCart: addToCart),24CartWidget(items: cartItems),25CartBadge(count: cartItems.length),26],27);28}29}3031// Child receives state via constructor32class CartBadge extends StatelessWidget {33final int count;34const CartBadge({required this.count});3536@override37Widget build(BuildContext context) {38return Badge(39label: Text('$count'),40child: Icon(Icons.shopping_cart),41);42}43}
Lift state when two or more widgets need to read/write the same data. Keep state as close as possible to where it is used.
Lifting state is useful, but lifting too high creates noise.
If many unrelated widgets receive values they do not use, your state is probably too high in the tree. Move shared state to feature-level container widgets or state providers closer to usage.
Share state between widgets properly. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.
Connect each concept to real app features you can build.
Sketch the widget tree first, then code from top to bottom.
Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.
Shared state goes in the lowest common ancestor of all widgets that need it.
Prop drilling means passing data through many intermediate widgets that don't need it, just to reach a deep child.
Parent passes callback functions (like onAddToCart) to children, who call them to modify the parent's state.
Lifting state too high requires passing it through many intermediate widgets, making code harder to maintain.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue