Skip to content

🛑 UiMode.adaptive in AdaptiveDropdownSearch causes dependOnInheritedWidgetOfExactType assertion #777

@Erengun

Description

@Erengun

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

  1. Clone the official repo:

    git clone https://github.com/salim-lachdhaf/dropdown_search.git
    cd dropdown_search
    
  2. Run the example app:

    cd example
    flutter pub get
    flutter run
    
  3. In the app's UI, select the "Adaptive Example" screen.

  4. Tap the "UI mode" dropdown and choose "adaptive".

  5. 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(),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions