Challenge - 5 Problems
BLoC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when the BLoC emits a new state?
In a Flutter app using the BLoC pattern, what is the expected behavior when the BLoC emits a new state?
Attempts:
2 left
💡 Hint
Think about how Flutter widgets react to state changes.
✗ Incorrect
In the BLoC pattern, when a new state is emitted, widgets listening to the BLoC rebuild automatically to show the updated UI.
🧠 Conceptual
intermediate2:00remaining
What is the role of events in the BLoC pattern?
In the BLoC pattern, what is the main purpose of events?
Attempts:
2 left
💡 Hint
Events are like messages sent to the BLoC.
✗ Incorrect
Events are inputs to the BLoC that tell it what action to perform, leading to new states being emitted.
❓ lifecycle
advanced2:00remaining
What is the correct way to close a BLoC to avoid memory leaks?
In Flutter, when using the BLoC pattern, how should you properly close a BLoC instance to prevent memory leaks?
Attempts:
2 left
💡 Hint
Think about widget lifecycle and resource cleanup.
✗ Incorrect
Closing the BLoC in dispose() ensures streams are closed and resources freed when the widget is removed.
📝 Syntax
advanced2:00remaining
What is the output of this BLoC event handling code?
Given this simplified BLoC event handler code, what will be the final state emitted?
code:
"""
class CounterBloc extends Bloc {
CounterBloc() : super(0) {
on((event, emit) => emit(state + 1));
on((event, emit) => emit(state - 1));
}
}
final bloc = CounterBloc();
bloc.add(Increment());
bloc.add(Increment());
bloc.add(Decrement());
"""
Attempts:
2 left
💡 Hint
Count how many increments and decrements happen.
✗ Incorrect
Starting at 0, two increments add 2, then one decrement subtracts 1, resulting in 1.
🔧 Debug
expert2:00remaining
Why does this BLoC code cause a runtime error?
Consider this BLoC code snippet:
code:
"""
class MyBloc extends Bloc {
MyBloc() : super(InitialState()) {
on((event, emit) async {
await Future.delayed(Duration(seconds: 1));
emit(NewState());
});
}
}
final bloc = MyBloc();
bloc.add(MyEvent());
bloc.close();
"""
Why might this code cause a runtime error?
Attempts:
2 left
💡 Hint
Think about what happens when you close a BLoC while it is still processing.
✗ Incorrect
Closing the BLoC immediately after adding the event causes the async emit to try updating a closed stream, leading to an error.