Complete the code to create a StatefulWidget class named MyWidget.
class MyWidget extends [1] { @override State<MyWidget> createState() => _MyWidgetState(); }
The class must extend StatefulWidget to create a widget that can change its state.
Complete the code to define the State class for MyWidget.
class _MyWidgetState extends [1]<MyWidget> { @override Widget build(BuildContext context) { return Container(); } }
The State class must extend State with the widget type as a generic parameter.
Fix the error in the setState call to update the counter.
int counter = 0; void increment() { [1](() { counter++; }); }
Use setState to tell Flutter the state changed and the UI should update.
Fill both blanks to complete the StatefulWidget and its State class skeleton.
class MyCounter extends [1] { @override [2]<MyCounter> createState() => _MyCounterState(); } class _MyCounterState extends State<MyCounter> { @override Widget build(BuildContext context) { return Container(); } }
The widget class extends StatefulWidget, and the createState method returns a State object.
Fill all three blanks to complete a StatefulWidget that updates a counter on button press.
class CounterWidget extends [1] { @override State<CounterWidget> createState() => _CounterWidgetState(); } class _CounterWidgetState extends State<CounterWidget> { int count = 0; void increment() { [2](() { count [3] 1; }); } @override Widget build(BuildContext context) { return ElevatedButton( onPressed: increment, child: Text('Count: $count'), ); } }
The widget extends StatefulWidget. The setState method updates the UI, and += increments the count.