Step 1 / 6
Two classes, two lifetimes
A StatefulWidget is still immutable. It creates a separate State object with createState(), and that object holds anything that changes.
Hold changing data in a State object, call setState to rebuild, and follow the widget lifecycle.
1class Counter extends StatefulWidget {2 const Counter({super.key});34 @override5 State<Counter> createState() => _CounterState();6}78class _CounterState extends State<Counter> {9 int _count = 0;1011 void _increment() => setState(() => _count++);1213 @override14 Widget build(BuildContext context) {15 return FilledButton(16 onPressed: _increment,17 child: Text('Tapped $_count times'),18 );19 }20}
A StatefulWidget is still immutable. It creates a separate State object with createState(), and that object holds anything that changes.