0
0
Fluttermobile~20 mins

Provider package in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Provider Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Understanding Provider Consumer Widget Behavior
Given the following Flutter code using Provider, what will be the output on the screen when the button is pressed once?
Flutter
class Counter extends ChangeNotifier {
  int value = 0;
  void increment() {
    value++;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => Counter(),
      child: MaterialApp(
        home: Scaffold(
          body: Center(
            child: Consumer<Counter>(
              builder: (context, counter, _) => Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Count: ${counter.value}'),
                  ElevatedButton(
                    onPressed: counter.increment,
                    child: Text('Increment'),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
AThe screen shows 'Count: 1' after pressing the button once.
BThe screen shows 'Count: 0' even after pressing the button once.
CThe app crashes with a runtime error when the button is pressed.
DThe screen shows 'Count: null' after pressing the button once.
Attempts:
2 left
💡 Hint
Remember that notifyListeners() triggers UI update in Consumer widgets.
lifecycle
intermediate
2:00remaining
Provider Lifecycle and Disposal
What happens to the ChangeNotifier instance provided by ChangeNotifierProvider when the widget tree containing it is removed?
AThe ChangeNotifier instance is automatically disposed to free resources.
BThe ChangeNotifier instance remains in memory causing a memory leak.
CThe ChangeNotifier instance throws an error when the widget is removed.
DThe ChangeNotifier instance resets its state but is not disposed.
Attempts:
2 left
💡 Hint
Think about how ChangeNotifierProvider manages the lifecycle of the object it creates.
📝 Syntax
advanced
2:00remaining
Correct Usage of Provider.of with listen Parameter
What is the output of this code snippet inside a widget's build method? final counter = Provider.of(context, listen: false); Text('Count: ${counter.value}')
Flutter
class Counter {
  int value = 5;
}

// Inside build method:
final counter = Provider.of<Counter>(context, listen: false);
return Text('Count: ${counter.value}');
ADisplays 'Count: null' because listen: false disables access.
BDisplays 'Count: 5' and rebuilds automatically when Counter changes.
CThrows a runtime error because listen: false is invalid here.
DDisplays 'Count: 5' and does NOT rebuild when Counter changes.
Attempts:
2 left
💡 Hint
listen: false means the widget won't rebuild on changes.
🔧 Debug
advanced
2:00remaining
Debugging Provider Not Updating UI
Why does the UI NOT update when the following ChangeNotifier's value changes? class Counter with ChangeNotifier { int value = 0; void increment() { value++; // Missing notifyListeners() } }
Flutter
class Counter extends ChangeNotifier {
  int value = 0;
  void increment() {
    value++;
    // notifyListeners() is missing here
  }
}
ABecause increment() is not called inside setState(), UI won't update.
BBecause value is private, the UI cannot access it.
CBecause notifyListeners() is not called, the UI does not rebuild on changes.
DBecause ChangeNotifier is not extended properly, UI won't update.
Attempts:
2 left
💡 Hint
Check if the notifier tells listeners about changes.
🧠 Conceptual
expert
2:00remaining
Provider Scope and Access
If you have two nested ChangeNotifierProviders providing different Counter instances, which instance will Provider.of(context) return inside the inner widget?
AA runtime error occurs due to multiple providers of the same type.
BThe Counter instance from the closest (inner) ChangeNotifierProvider in the widget tree.
CThe Counter instance from the outer ChangeNotifierProvider only.
DIt returns null because of ambiguity between providers.
Attempts:
2 left
💡 Hint
Provider.of finds the nearest matching provider up the widget tree.