Sample questions
Question 1Easy
In the BLoC pattern, how does the UI interact with a Bloc?
- It adds events and rebuilds from the states the Bloc emits
- It passes BuildContext to the Bloc so the Bloc can build widgets
- It reads the Bloc's private fields and mutates them in place
- It calls setState inside the Bloc to change its widgets directly
Answer: A. It adds events and rebuilds from the states the Bloc emits
Explanation
Input events flow into the Bloc, which processes them and emits new immutable states; widgets such as BlocBuilder rebuild from those states.
Step-by-step solution
- UI adds events to trigger Bloc actions.
- Bloc processes events and emits new states.
- Widgets like BlocBuilder rebuild using the emitted states.
- This flow ensures UI updates based on Bloc state changes.
- Final answer: It adds events and rebuilds from the states the Bloc emits.
Why the other options are wrong
- B. The Bloc does not receive BuildContext to build widgets; the UI reads states and sends events.
- C. Bloc state is not mutated in place; the UI reads emitted states and never edits private fields.
- D. The Bloc should not call setState on widgets; the UI rebuilds from emitted states via BlocBuilder.
Tip: Remember: UI triggers actions, Bloc updates state, widgets rebuild based on state.
Question 2Easy
Two sibling widgets need the same changing value. What does "lifting state up" mean?
- Moving the state to their nearest common ancestor
- Storing the state in each widget's key so it can be read
- Moving the state into a global variable outside any widget
- Copying the state into both widgets and syncing it on a timer
Answer: A. Moving the state to their nearest common ancestor
Explanation
The ancestor owns the value and passes it down through constructors, along with callbacks that let children ask for changes.
Step-by-step solution
- State is owned by the nearest common ancestor.
- The ancestor passes the state to both widgets via props.
- Widgets use callbacks to request updates from the ancestor.
- This ensures both widgets share the same state without duplication.
- Final answer: Moving the state to their nearest common ancestor.
Why the other options are wrong
- B. Keys identify widgets; storing state in a key does not share a value between siblings.
- C. A global variable works but is not lifting state; the state belongs to the nearest common ancestor.
- D. Copies that sync on a timer risk drift and are not lifting state; the state has one owner instead.
Tip: Share state by lifting it up to the closest common parent.
Question 3Easy
In the BLoC pattern, what does BLoC stand for?
- Business Logic Component
- Basic Layout of Components
- Build Loop Controller
- Binary Layer of Cache
Answer: A. Business Logic Component
Explanation
BLoC separates business logic from UI: the UI sends events in and receives states out, typically as streams.
Step-by-step solution
- BLoC is an acronym used in Flutter architecture.
- Each letter stands for a word in the phrase Business Logic Component.
- The pattern separates business logic from UI, matching the acronym.
Why the other options are wrong
- B. Layout is not what BLoC refers to; the name comes from Business Logic Component.
- C. BLoC is not a build loop; the name stands for Business Logic Component.
- D. BLoC is not a cache layer; the pattern separates business logic from the UI.
Tip: B = Business, L = Logic, C = Component.
Question 4Easy
The Provider package is mainly a convenient wrapper around which Flutter mechanism?
- MethodChannel
- InheritedWidget
- StreamController
- GlobalKey
Answer: B. InheritedWidget
Explanation
Provider makes InheritedWidget easier to use, adding lifecycle handling and selective rebuilds.
Step-by-step solution
- Provider simplifies the usage of InheritedWidget by managing its lifecycle automatically.
- It also handles selective rebuilding of widgets based on state changes.
- This makes InheritedWidget more efficient and easier to work with in Flutter apps.
- Therefore, Provider is a convenient wrapper around InheritedWidget.
- The correct answer is B (InheritedWidget).
Why the other options are wrong
- A. MethodChannel is for Dart-to-native messaging; Provider does not wrap it.
- C. StreamController manages Dart streams; Provider is built on InheritedWidget, not a stream.
- D. GlobalKey gives access to a widget's State; Provider uses InheritedWidget to share values down the tree.
Tip: Remember, Provider wraps InheritedWidget for easier state management in Flutter.
Question 5Medium
What is a key design difference between Riverpod and Provider?
- Riverpod stores all state in native code on Android and iOS
- Riverpod providers are global and don't need the widget tree
- Riverpod requires each provider to be a subclass of InheritedWidget
- Riverpod can only manage local state for a single StatefulWidget
Answer: B. Riverpod providers are global and don't need the widget tree
Explanation
Riverpod providers are top-level objects, read through a ref. This avoids looking up types through BuildContext and the ProviderNotFound errors that come with it.
Step-by-step solution
- Provider requires a BuildContext to find values in the widget tree.
- Riverpod providers are defined as top-level global objects.
- You access Riverpod state using a ref without needing the widget tree.
- This removes the need to search for types via BuildContext.
- The key difference is that Riverpod providers are global and don't need the widget tree.
Why the other options are wrong
- A. Riverpod is not tied to native code on Android or iOS; it is a pure Dart state library.
- C. Riverpod providers are not InheritedWidget subclasses; Provider is the package built on InheritedWidget.
- D. Riverpod is not limited to a single StatefulWidget; it manages app-wide and local state alike.
Tip: Remember that Riverpod uses global references while Provider uses the widget tree.
Question 6Medium
A widget only uses user.name from a large model, but rebuilds whenever any field changes. Which Provider feature narrows the rebuilds?
- context.select or Selector
- A GlobalKey on the widget
- context.read inside build
- Wrapping it in a RepaintBoundary
Answer: A. context.select or Selector
Explanation
select listens to one derived value and only rebuilds when that value changes, instead of on every notification from the model.
Step-by-step solution
- Select watches only user.name, not other fields.
- Only user.name change triggers rebuild.
- Other field changes do not affect widget.
- This reduces unnecessary rebuilds.
- Final answer: context.select or Selector.
Why the other options are wrong
- B. A GlobalKey gives a widget a handle to its State; it does not limit what triggers a rebuild.
- C. read does not subscribe to changes, so it cannot narrow rebuilds for a value the widget displays.
- D. RepaintBoundary isolates painting, not Provider rebuilds; it does not select fields from the model.
Tip: Use select for widgets that depend on a single part of the state.
Question 7Medium
When an InheritedWidget is replaced with a new instance, which descendants rebuild?
- Only the InheritedWidget's direct child, and never any deeper descendants
- Every widget in the app, whether or not it reads any value from the InheritedWidget
- No widgets rebuild unless each one calls setState after the InheritedWidget changes
- Those that registered a dependency, if updateShouldNotify is true
Answer: D. Those that registered a dependency, if updateShouldNotify is true
Explanation
Calling dependOnInheritedWidgetOfExactType registers a dependency. If updateShouldNotify(old) returns true, only those dependents are rebuilt.
Step-by-step solution
- Widgets that read the InheritedWidget register as dependents.
- When it is replaced, updateShouldNotify decides whether to notify.
- If true, only the registered dependents rebuild.
Why the other options are wrong
- A. Depth does not matter, only registration.
- B. Only dependents rebuild, not the whole app.
- C. Dependents rebuild automatically without setState.
Tip: Dependents + updateShouldNotify.
Question 8Medium
With Provider, what is the difference between context.watch<T>() and context.read<T>()?
- read rebuilds the widget on changes; watch just gets the value once
- watch rebuilds the widget on changes; read just gets the value once
- watch works only inside callbacks, and read works only inside build
- There is no difference other than the name of the method used
Answer: B. watch rebuilds the widget on changes; read just gets the value once
Explanation
watch subscribes the widget, so use it in build. read does not listen, so it is meant for callbacks like onPressed.
Step-by-step solution
- Watch subscribes to changes, so the widget rebuilds when the value changes.
- Read gets the value once and does not rebuild the widget.
- So watch rebuilds and read only gets the value once, which is option B.
Why the other options are wrong
- A. The two names are swapped: read does not rebuild and watch does.
- C. Neither method is limited to callbacks or to build.
- D. Watch and read behave differently; watch rebuilds on changes.
Tip: Use watch inside build to show changing values; use read inside callbacks.
Question 9Hard
A Cubit holds a List<String> as its state. A method runs state.add(item); emit(state); but the UI does not update. Why?
- emit ignores a state equal to the current one: same list
- emit can only be called from inside an asynchronous Cubit method
- The UI must call setState after every emit to see the change
- Lists are not allowed to be used as state in a Cubit or Bloc
Answer: A. emit ignores a state equal to the current one: same list
Explanation
Bloc ignores an emitted state that equals the current state. Mutating the list in place means the new state is the same object; emit a new list such as [...state, item].
Step-by-step solution
- Adding to the list changes its contents but not its identity.
- Emit compares the new state with the current one by reference.
- Same list object means emit ignores the change.
- To trigger UI update, a new list must be emitted.
- Final answer: emit ignores a state equal to the current one: same list.
Why the other options are wrong
- B. emit works from any method, sync or async; the problem is the unchanged list identity.
- C. A Cubit does not need setState; the UI rebuilds from emitted states through BlocBuilder.
- D. Lists are allowed as state; the fix is to emit a new list instance with the added item.
Tip: Always emit a new list, not modify the existing one.
Question 10Hard
After await api.save(); an event handler calls Navigator.of(context).pop(). Why do lints warn about using BuildContext across an async gap?
- BuildContext is copied when awaiting, so pop would affect a different route
- Navigator calls are not allowed inside asynchronous functions in Flutter
- The widget may be unmounted by then; check mounted first
- Awaiting a Future releases the BuildContext so it is null when resumed
Answer: C. The widget may be unmounted by then; check mounted first
Explanation
While awaiting, the user can leave the screen and the element is disposed. Checking context.mounted (or State.mounted) before using it avoids errors.
Step-by-step solution
- BuildContext is tied to a widget's lifecycle, which may end during await.
- If the widget is unmounted, context becomes invalid and causes errors.
- Checking
context.mounted ensures the context is still valid before using it. - Using
Navigator.of(context).pop() without checking can crash if the widget is gone. - The correct answer highlights checking mounted to avoid errors.
Why the other options are wrong
- A. BuildContext is not copied during await; the risk is that the widget may have been removed.
- B. Navigator calls work fine in async functions; the lint is about the context being stale.
- D. Awaiting does not null the BuildContext; the context stays a reference, though the widget may be gone.
Tip: Always check context.mounted when using BuildContext in async functions.
Practise all 15 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →