Sample questions
Fl Flutter State Management WidgetsDifficulty 1
What is the primary purpose of the initState() method in a State object?
- aOne-time setup that runs once when the State object is first inserted into the tree.✓
- bCode that runs every time the widget rebuilds.
- cA method that tears down resources before the widget is removed.
- dA callback invoked only when the parent widget's constructor arguments change.
Explanation:initState() is called once, immediately after the State object is created and inserted into the tree — the right place for one-time setup like subscribing to a stream or initializing a controller.
Fl Flutter State Management WidgetsDifficulty 1
What is dispose() responsible for in a State object?
- aReleasing resources before State is removed.✓
- bRebuilding the widget tree from scratch.
- cRunning before every single
build() call. - dPersisting the widget's state to disk automatically.
Explanation:dispose() runs once, right before the State object is discarded (e.g. when the widget is permanently removed from the tree) — the place to cancel stream subscriptions, dispose AnimationControllers, close TextEditingControllers, etc.
Fl Flutter State Management WidgetsDifficulty 1
A widget calls setState(() { count++; }) inside its build method's returned callback. What does setState actually cause?
- aIt immediately repaints the pixel buffer without calling
build() again. - bIt only updates the variable; the UI never reflects the change.
- cIt rebuilds every widget in the entire app, not just this one.
- dIt marks the widget dirty and schedules a rebuild so
build() runs again.✓
Explanation:setState runs the given callback (mutating the field) and then tells the framework this widget is dirty, scheduling it to be rebuilt on the next frame — build() is called again and the new value is reflected in the returned widget tree.
Fl Flutter State Management WidgetsDifficulty 2
Why does calling setState outside a State object's own lifetime (e.g. after dispose()) throw an error?
- aBecause
setState is a static method that ignores object identity entirely. - bBecause Dart forbids calling any method after a class instance is created.
- cBecause the widget's constructor arguments become immutable after dispose.
- dBecause the State no longer has an attached element to schedule a rebuild for.✓
Explanation:Once dispose() has run, the State is detached from the element tree — there is no longer a valid element to mark dirty, so calling setState at that point is a lifecycle violation and throws.
Fl Flutter State Management WidgetsDifficulty 2
A StatefulWidget's parent rebuilds and passes new constructor arguments to the same widget (same position/type, same Key). Which State lifecycle method is called to let the State react to the new arguments?
- a
initState(), because a whole new State object is always created. - b
didUpdateWidget(oldWidget), giving access to the previous configuration.✓ - cNo method is called; the State object can never see the new constructor arguments.
- d
dispose(), since new arguments count as removal of the old widget.
Explanation:When a widget is rebuilt with the same runtime type and Key, Flutter reuses the same State object and calls didUpdateWidget(oldWidget) so it can compare oldWidget against the new widget field and react (e.g. re-subscribe if a passed-in stream changed).
Fl Flutter State Management WidgetsDifficulty 1
What problem does InheritedWidget solve?
- aIt lets a descendant read an ancestor's value without explicit constructor passing.✓
- bIt replaces the need for a
build() method entirely. - cIt permanently stores data in the device's local database.
- dIt forces every widget in the subtree to rebuild every frame regardless of changes.
Explanation:InheritedWidget is Flutter's mechanism for efficient, implicit value propagation down the tree — a descendant calls context.dependOnInheritedWidgetOfExactType<T>() to read the value and register itself to be notified if that value changes, without manual prop-drilling.