Skip to content

Commit 0205d4e

Browse files
Make DropdownMenu generic type non nullable (#176711)
Fixes flutter/flutter#175090 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 04ccef3 commit 0205d4e

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

packages/flutter/lib/src/material/dropdown_menu.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ enum DropdownMenuCloseBehavior {
151151
/// The [DropdownMenu] uses a [TextField] as the "anchor".
152152
/// * [TextField], which is a text input widget that uses an [InputDecoration].
153153
/// * [DropdownMenuEntry], which is used to build the [MenuItemButton] in the [DropdownMenu] list.
154-
class DropdownMenu<T> extends StatefulWidget {
154+
class DropdownMenu<T extends Object> extends StatefulWidget {
155155
/// Creates a const [DropdownMenu].
156156
///
157157
/// The leading and trailing icons in the text field can be customized by using
@@ -386,7 +386,12 @@ class DropdownMenu<T> extends StatefulWidget {
386386

387387
/// The callback is called when a selection is made.
388388
///
389-
/// Defaults to null. If null, only the text field is updated.
389+
/// The callback receives the selected entry's value of type `T` when the user
390+
/// chooses an item. It may also be invoked with `null` to indicate that the
391+
/// selection was cleared / that no item was chosen.
392+
///
393+
/// Defaults to null. If this callback itself is null, the widget still updates
394+
/// the text field with the selected label.
390395
final ValueChanged<T?>? onSelected;
391396

392397
/// Defines the keyboard focus for this widget.
@@ -606,7 +611,7 @@ class DropdownMenu<T> extends StatefulWidget {
606611
State<DropdownMenu<T>> createState() => _DropdownMenuState<T>();
607612
}
608613

609-
class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
614+
class _DropdownMenuState<T extends Object> extends State<DropdownMenu<T>> {
610615
final GlobalKey _anchorKey = GlobalKey();
611616
final GlobalKey _leadingKey = GlobalKey();
612617
late List<GlobalKey> buttonItemKeys;

packages/flutter/lib/src/material/dropdown_menu_form_field.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import 'menu_style.dart';
2727
///
2828
/// * [DropdownMenu], which is the underlying text field without the [Form]
2929
/// integration.
30-
class DropdownMenuFormField<T> extends FormField<T> {
30+
class DropdownMenuFormField<T extends Object> extends FormField<T> {
3131
/// Creates a [DropdownMenu] widget that is a [FormField].
3232
///
3333
/// For a description of the `onSaved`, `validator`, or `autovalidateMode`
@@ -117,7 +117,12 @@ class DropdownMenuFormField<T> extends FormField<T> {
117117

118118
/// The callback is called when a selection is made.
119119
///
120-
/// Defaults to null. If null, only the text field is updated.
120+
/// The callback receives the selected entry's value of type `T` when the user
121+
/// chooses an item. It may also be invoked with `null` to indicate that the
122+
/// selection was cleared / that no item was chosen.
123+
///
124+
/// Defaults to null. If this callback itself is null, the widget still updates
125+
/// the text field with the selected label.
121126
final ValueChanged<T?>? onSelected;
122127

123128
/// Controls the text being edited.
@@ -136,7 +141,7 @@ class DropdownMenuFormField<T> extends FormField<T> {
136141
FormFieldState<T> createState() => _DropdownMenuFormFieldState<T>();
137142
}
138143

139-
class _DropdownMenuFormFieldState<T> extends FormFieldState<T> {
144+
class _DropdownMenuFormFieldState<T extends Object> extends FormFieldState<T> {
140145
DropdownMenuFormField<T> get _dropdownMenuFormField => widget as DropdownMenuFormField<T>;
141146

142147
// The controller used to restore the selected item.

0 commit comments

Comments
 (0)