This app shows a number on screen that increases each time you press the button. The BLoC handles the counting logic and updates the UI through a stream.
Calling addNumber(5) adds 5 to the stream sink, which emits 5 to listeners.
Step 2: StreamBuilder updates UI
StreamBuilder listens to stream and rebuilds Text widget with snapshot.data = 5.
Final Answer:
5 -> Option A
Quick Check:
Stream emits 5, UI shows 5 [OK]
Hint: StreamBuilder shows latest stream data in UI [OK]
Common Mistakes:
Thinking UI shows 'No data' after adding a number
Confusing snapshot.data with snapshot.hasData
Assuming null is shown instead of actual data
4. Identify the error in this BLoC snippet:
class CounterBloc {
final _controller = StreamController<int>();
void increment() {
_controller.sink.add(_counter + 1);
}
Stream<int> get stream => _controller.stream;
}
Assuming _counter is not declared anywhere, what is the issue?
medium
A. increment() should return a value
B. StreamController is not closed properly
C. Stream getter should be a method, not a property
D. _counter is not defined, causing a compile error
Solution
Step 1: Check variable usage
_counter is used but never declared or initialized, causing an error.
Step 2: Review other options
While closing StreamController is good practice, missing _counter causes immediate compile error.
Final Answer:
_counter is not defined, causing a compile error -> Option D
Quick Check:
Undefined variable = compile error [OK]
Hint: Undefined variables cause compile errors immediately [OK]
Common Mistakes:
Ignoring missing variable declarations
Confusing Stream getter syntax
Thinking StreamController must always be closed immediately
5. You want to create a BLoC that filters out even numbers and only emits odd numbers to the UI. Which code snippet correctly implements this behavior?
hard
A. final _controller = StreamController<int>();
Stream<int> get stream => _controller.stream.skip(1);
B. final _controller = StreamController<int>();
Stream<int> get stream => _controller.stream.map((n) => n * 2);
C. final _controller = StreamController<int>();
Stream<int> get stream => _controller.stream.where((n) => n % 2 != 0);
D. final _controller = StreamController<int>();
Stream<int> get stream => _controller.stream.where((n) => n % 2 == 0);
Solution
Step 1: Understand filtering logic
We want to emit only odd numbers, so filter with condition n % 2 != 0.
Step 2: Check each option's stream transformation
Only the transformation using where((n) => n % 2 != 0) correctly filters odd numbers. The others either skip the first element, double the numbers with map, or filter even numbers with where((n) => n % 2 == 0).
Final Answer:
final _controller = StreamController<int>();\nStream<int> get stream => _controller.stream.where((n) => n % 2 != 0); -> Option C
Quick Check:
Filter odd numbers with where(n % 2 != 0) [OK]
Hint: Use where() with condition to filter stream data [OK]
Common Mistakes:
Using map() instead of where() for filtering
Filtering even numbers instead of odd
Using skip() which removes elements by count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.