From 7eeab2666b1ee1b4fac8604835f7ba903e0cded1 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Wed, 14 Oct 2020 16:52:03 +0800 Subject: [PATCH] Fix DropdownButton bug (#65915) --- packages/flutter/lib/src/material/dropdown.dart | 16 ++++++---------- .../flutter/test/material/dropdown_test.dart | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) 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(); },