-
-
Notifications
You must be signed in to change notification settings - Fork 371
Description
Describe the bug
The package crashes on startup when using UiMode.adaptive because PlatformUi.getUiToApply() invokes Theme.of(context) inside initState.
That call occurs in lib/src/base_dropdown_search.dart:310,
before the widget has a chance to subscribe to the inherited theme, triggering a Flutter assertion about dependOnInheritedWidgetOfExactType
🧪 Steps to Reproduce
-
Clone the official repo:
git clone https://github.com/salim-lachdhaf/dropdown_search.git cd dropdown_search
-
Run the example app:
cd example flutter pub get flutter run
-
In the app's UI, select the "Adaptive Example" screen.
-
Tap the "UI mode" dropdown and choose "adaptive".
-
Observe the crash and the
FlutterError
on console:
FlutterError (dependOnInheritedWidgetOfExactType<_InheritedTheme>() or dependOnInheritedElement() was called before DropdownSearchState<String>.initState() completed.
When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.)
📌 Actual Behavior
When UiMode.adaptive
is selected, the example crashes immediately due to Theme.of(context)
being called too early in initState
, specifically in:
lib/src/base_dropdown_search.dart:310
PlatformUi.getUiToApply(…) → calls Theme.of(this)
✅ Expected Behavior
Switching to adaptive
should not crash the example. Theme lookups must be deferred to didChangeDependencies()
, so it properly subscribes to theme changes.
💡 Suggested Fix
Move the UI resolution out of initState()
into didChangeDependencies()
,