0
0
Fluttermobile~20 mins

BLoC pattern basics in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BLoC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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?
AThe UI rebuilds automatically to reflect the new state.
BThe app crashes because states cannot be emitted twice.
CNothing happens until the user restarts the app.
DThe BLoC stops listening to events.
Attempts:
2 left
💡 Hint
Think about how Flutter widgets react to state changes.
🧠 Conceptual
intermediate
2:00remaining
What is the role of events in the BLoC pattern?
In the BLoC pattern, what is the main purpose of events?
AEvents store the UI layout information.
BEvents trigger the BLoC to process logic and emit new states.
CEvents are used to style widgets dynamically.
DEvents automatically update the database.
Attempts:
2 left
💡 Hint
Events are like messages sent to the BLoC.
lifecycle
advanced
2: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?
ACall the close() method on the BLoC inside the dispose() method of the widget.
BDo nothing; Flutter automatically cleans up BLoCs.
CCall the close() method inside the build() method.
DRecreate the BLoC every time the widget rebuilds.
Attempts:
2 left
💡 Hint
Think about widget lifecycle and resource cleanup.
📝 Syntax
advanced
2: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()); """
A-1
B2
C0
D1
Attempts:
2 left
💡 Hint
Count how many increments and decrements happen.
🔧 Debug
expert
2: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?
ABecause the event is not dispatched on the main thread.
BBecause the event handler is not marked async.
CBecause the BLoC is closed immediately after adding the event, the async emit tries to update a closed stream.
DBecause the initial state is missing a required parameter.
Attempts:
2 left
💡 Hint
Think about what happens when you close a BLoC while it is still processing.