diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 5dea30b244..8fb994601b 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1303,16 +1303,12 @@ class _DropdownButtonState extends State> with WidgetsBindi // The width of the button and the menu are defined by the widest // item and the width of the hint. - List items; - if (_enabled) { - items = widget.selectedItemBuilder == null - ? List.from(widget.items!) - : widget.selectedItemBuilder!(context); - } else { - items = widget.selectedItemBuilder == null - ? [] - : widget.selectedItemBuilder!(context); - } + // We should explicitly type the items list to be a list of , + // otherwise, no explicit type adding items maybe trigger a crash/failure + // when hint and selectedItemBuilder are provided. + final List items = widget.selectedItemBuilder == null + ? (_enabled ? List.from(widget.items!) : []) + : List.from(widget.selectedItemBuilder!(context)); int? hintIndex; if (widget.hint != null || (!_enabled && widget.disabledHint != null)) { diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index a49aea6739..6125564813 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -1945,7 +1945,7 @@ void main() { }); }, selectedItemBuilder: (BuildContext context) { - return items.map((String item) { + return items.map((String item) { return Text('You have selected: $item'); }).toList(); },