0
0
Fluttermobile~20 mins

StatefulWidget in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StatefulWidget Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Flutter StatefulWidget code?
Consider this Flutter app code. What text will be shown on the screen after tapping the button twice?
Flutter
import 'package:flutter/material.dart';

class CounterApp extends StatefulWidget {
  @override
  State<CounterApp> createState() => _CounterAppState();
}

class _CounterAppState extends State<CounterApp> {
  int count = 0;

  void increment() {
    setState(() {
      count += 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Count: $count'),
              ElevatedButton(
                onPressed: increment,
                child: Text('Add'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
ACount: 3
BCount: 1
CCount: 0
DCount: 2
Attempts:
2 left
💡 Hint
Each tap increases count by 1 and updates the UI.
lifecycle
intermediate
1:30remaining
Which lifecycle method is called only once when a StatefulWidget is inserted into the widget tree?
In Flutter StatefulWidget lifecycle, which method runs exactly once when the widget is first created?
AinitState()
Bbuild()
Cdispose()
DdidUpdateWidget()
Attempts:
2 left
💡 Hint
This method is used to initialize data before the widget appears.
📝 Syntax
advanced
2:00remaining
What error does this Flutter StatefulWidget code produce?
Look at this code snippet. What error will it cause when compiling?
Flutter
class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() {
    return _MyWidgetState();
  }
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return Text('Hello');
  }

  void setState(VoidCallback fn) {
    // empty override
  }
}
AError: setState() method override with wrong signature
BNo error, runs fine
CError: Missing build method
DError: createState() must be static
Attempts:
2 left
💡 Hint
setState() has a specific signature and purpose in State class.
navigation
advanced
1:30remaining
What happens to StatefulWidget state when navigating back to it using Navigator.pop()?
If you navigate from Screen A (StatefulWidget) to Screen B, then return to Screen A using Navigator.pop(), what happens to Screen A's state?
AState is lost and causes an error
BState is preserved as Screen A was not destroyed
CState is saved to disk and restored automatically
DState is reset because Screen A is rebuilt from scratch
Attempts:
2 left
💡 Hint
Navigator.pop() removes the top screen and reveals the previous one.
🔧 Debug
expert
2:30remaining
Why does this StatefulWidget not update UI after setState call?
This code calls setState but the UI text does not change. Why? class MyWidget extends StatefulWidget { @override State createState() => _MyWidgetState(); } class _MyWidgetState extends State { String text = 'Hello'; void changeText() { setState(() { text = 'Hello'; }); } @override Widget build(BuildContext context) { return Text(text); } }
AText widget does not rebuild on setState
BsetState must be called outside the State class
CsetState called but state value did not change, so UI not updated
DMissing call to super.setState() inside setState
Attempts:
2 left
💡 Hint
UI updates only if state variables change inside setState.