Sample questions
Question 1Easy
What is a typical use of a GlobalKey?
- Reaching a widget's State from elsewhere, e.g. FormState
- Making a widget appear on every screen of the app
- Giving every list item a stable identity among its siblings
- Storing a value in the device's secure key storage
Answer: A. Reaching a widget's State from elsewhere, e.g. FormState
Explanation
A GlobalKey is unique across the app and exposes currentState and currentContext, e.g. _formKey.currentState!.validate().
Step-by-step solution
- GlobalKey uniquely identifies a widget in the app.
- It allows access to the widget's state from outside, like FormState.
- Using GlobalKey, you can call methods on the widget's state directly.
- This is useful for validating forms or accessing context from elsewhere.
- The correct use matches option A.
Why the other options are wrong
- B. A GlobalKey does not put a widget on every screen; it gives one widget a unique, reachable identity.
- C. Sibling identity is the job of a ValueKey or ObjectKey in a list, not a GlobalKey.
- D. GlobalKey is not secure storage; it is an identifier inside the widget tree, not a stored value.
Tip: Use GlobalKey to access widget state when it’s not directly reachable.
Question 2Easy
Should you give every widget a GlobalKey just in case?
- Yes, because it makes every rebuild faster than without any keys
- No, GlobalKeys are costly and should be used sparingly
- No, because GlobalKeys only work in release builds
- Yes, it is required for widgets to rebuild correctly
Answer: B. No, GlobalKeys are costly and should be used sparingly
Explanation
Each GlobalKey must be unique and is tracked in a global registry. Use local keys (ValueKey, ObjectKey) for list identity.
Step-by-step solution
- GlobalKeys are costly and must be unique.
- Use them sparingly.
Why the other options are wrong
- A. They don't speed up rebuilds.
- C. They work in all modes.
- D. They are not required.
Tip: GlobalKey = last resort.
Question 3Easy
What is a ValueKey based on?
- A random number generated on every rebuild
- The widget's position index within its parent
- A value, such as an item's id, compared with ==
- The widget's memory address in the Dart heap
Answer: C. A value, such as an item's id, compared with ==
Explanation
ValueKey(todo.id) gives the widget a stable identity tied to the data it represents.
Step-by-step solution
- ValueKey wraps a value, such as an item id.
- Flutter compares two ValueKeys with ==, so equal values match.
- So a ValueKey is based on a value compared with ==, which is option C.
Why the other options are wrong
- A. A random number would change on every build, so the keys would never match.
- B. The position index is not what ValueKey uses.
- D. The memory address is not used; keys are compared by value with ==.
Tip: Use ValueKey(item.id) on list items so Flutter can match them across rebuilds.
Question 4Easy
What is the main purpose of a Key on a widget?
- To keep identity and state when siblings move
- To make the widget accessible to screen readers
- To set the widget's order in the focus traversal
- To encrypt the widget's data when it is stored
Answer: A. To keep identity and state when siblings move
Explanation
Keys let the framework match old and new widgets among siblings, so state stays with the right item when items are reordered, added or removed.
Step-by-step solution
- Key helps track widget even if order changes.
- It keeps data linked to correct widget.
- State remains with right widget when siblings move.
- This ensures identity stays consistent.
- So Key maintains state and identity properly.
- Final answer: To keep identity and state when siblings move.
Why the other options are wrong
- B. Screen readers use Semantics, not a Key; a key does not make a widget accessible.
- C. Focus order is set by focus traversal settings, not by the widget's Key.
- D. Keys do not encrypt data; they only identify widgets during rebuilds.
Tip: Remember, Key keeps widget data safe during reordering.
Question 5Medium
How does ObjectKey differ from ValueKey?
ObjectKey is global across the app; ValueKey is local- There is no difference; one is an alias of the other
ObjectKey compares with identical(); ValueKey uses ==ValueKey compares with identical(); ObjectKey uses ==
Answer: C. ObjectKey compares with identical(); ValueKey uses ==
Explanation
ObjectKey ties identity to a specific object instance, while ValueKey uses the value's equality operator.
Step-by-step solution
ObjectKey relies on the identical() function to check identity.ValueKey relies on the == operator to check equality.- This means
ObjectKey matches only the exact same instance. - Meanwhile,
ValueKey matches any object with the same value. - Therefore,
ObjectKey compares with identical(); ValueKey uses ==.
Why the other options are wrong
- A. Both keys can be global or local; the difference is how they compare values, not their scope.
- B. They are different classes with different matching rules, so neither is an alias of the other.
- D. This swaps them: ValueKey uses ==, and ObjectKey uses identical(), not the other way round.
Tip: Use ValueKey for stable values and ObjectKey for unique instances.
Question 6Medium
Two StatefulWidget tiles without keys, each storing a random colour in its State, are swapped in a Row. What happens on screen?
- Both tiles get new random colours as their State is rebuilt
- The colours stay put, because State stays with position
- An exception is thrown, because sibling widgets must be keyed
- The colours swap correctly along with the two tile widgets
Answer: B. The colours stay put, because State stays with position
Explanation
Without keys, Flutter matches by type and position, so each position keeps its old State. Adding keys makes the State move with the widget.
Step-by-step solution
- Without keys, Flutter matches widgets by type and position.
- Each State stays at its position.
- The colours stay in place.
Why the other options are wrong
- A. States are not recreated.
- C. No exception is raised.
- D. State does not move with the widget without a key.
Tip: Add keys to follow the widget.
Question 7Medium
What happens if a widget is given key: UniqueKey() inside build?
- The key is generated once and reused on all later builds
- The widget becomes globally accessible from any other screen
- A new key each build, so its State is recreated every time
- The widget skips rebuilding because it has a unique identity
Answer: C. A new key each build, so its State is recreated every time
Explanation
Each UniqueKey only equals itself. Creating one in build means the key never matches the old one, so the element and State are rebuilt from scratch.
Step-by-step solution
- UniqueKey() creates a new key object each time it is called.
- Build runs often, so each build gives the widget a new key.
- Flutter sees a different key and discards the old State, creating a new one each build.
Why the other options are wrong
- A. The key is created on each call, not reused.
- B. A key does not make a widget accessible from other screens.
- D. A new key forces the State to be recreated, so the widget does not skip work.
Tip: Create a UniqueKey once in the State (for example as a final field), not inside build.
Question 8Medium
When does Flutter reuse an existing element for a new widget during a rebuild?
- When the new widget is identical to the old one in all its fields
- Only when the new widget was created with a const constructor
- Whenever the new widget appears at the same depth in the tree
- When the new widget has the same runtimeType and key as the old one
Answer: D. When the new widget has the same runtimeType and key as the old one
Explanation
Widget.canUpdate compares runtimeType and key. If they match, the element (and any State) is kept and updated with the new widget.
Step-by-step solution
- Flutter checks if the new widget has the same runtimeType as the old one.
- It also verifies if the new widget has the same key as the old one.
- If both match, Flutter reuses the existing element and updates it with the new widget.
- This ensures efficient rebuilds by avoiding unnecessary creation of new elements.
- The final answer is When the new widget has the same runtimeType and key as the old one.
Why the other options are wrong
- A. Element reuse does not require identical fields; the runtimeType and key decide it.
- B. const is not required for reuse; a non-const widget of the same type and key is still reused.
- C. Position in the tree is not enough; a different type or key at the same depth gets a new element.
Tip: Remember: runtimeType and key must match for element reuse in Flutter.
Question 9Hard
A stateful widget with a ValueKey is sometimes wrapped in a Padding and sometimes not, depending on a flag. What happens to its State when the flag flips?
- It is kept, because Padding does not count as a separate element
- It is lost; local keys only match siblings of one parent
- Flutter throws an error, because a keyed widget cannot change parent
- It is kept, because the ValueKey matches no matter where the widget is
Answer: B. It is lost; local keys only match siblings of one parent
Explanation
Local keys only help among siblings of the same parent. A change of parent type means the subtree is rebuilt; a GlobalKey can move state across parents.
Step-by-step solution
- Local keys work only for widgets with the same parent.
- Changing the parent breaks the key match.
- State is lost when the widget moves to a new parent.
Why the other options are wrong
- A. Padding is a real element in the tree, and the flag changes the parent, so the State is not kept.
- C. No error is thrown; Flutter simply creates a new State when the keyed widget moves to a new parent.
- D. A ValueKey does not match across parents; it only matches siblings, so the State is lost.
Tip: Local keys only work for same-parent siblings; changing parents loses state.
Question 10Hard
In one frame, a widget with a GlobalKey is removed from one parent and added under another. What does Flutter do?
- It throws, since a GlobalKey may never change its parent widget
- It moves the existing element and State to the new parent
- It disposes the State and creates a new one under the new parent
- It keeps both copies alive, one under each parent, for one frame
Answer: B. It moves the existing element and State to the new parent
Explanation
GlobalKeys allow reparenting: the element subtree, including State, is moved instead of rebuilt, as long as it moves within the same frame.
Step-by-step solution
- GlobalKey identifies the widget uniquely.
- Flutter moves the existing element and its State to the new parent.
- The key remains valid as long as the widget is part of the tree.
- This ensures no need to rebuild or dispose State unnecessarily.
- Final answer: It moves the existing element and State to the new parent.
Why the other options are wrong
- A. Moving a GlobalKey between parents in one frame is allowed; it is the supported use case.
- C. The State is not disposed and recreated; the GlobalKey keeps the same element and State alive.
- D. Both copies would not exist at once; a GlobalKey can be in only one place in the tree at a time.
Tip: GlobalKey allows reparenting without rebuilding or disposing State.
Practise all 12 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 →