Make DropdownMenu generic type non nullable#176711
Make DropdownMenu generic type non nullable#176711auto-submit[bot] merged 3 commits intoflutter:masterfrom
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
|
I have 2 questions:
|
There was a problem hiding this comment.
Code Review
This pull request makes the generic type T for DropdownMenu and DropdownMenuFormField non-nullable by adding an extends Object constraint. This is a good change for type safety, as it ensures that the value of a DropdownMenuEntry is always non-nullable within these widgets. The documentation for onSelected has also been updated to be more descriptive and clear, which is a great improvement. The changes are consistent and well-implemented across both dropdown_menu.dart and dropdown_menu_form_field.dart.
|
I'm in favor of making this change. import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: Card(
child: DropdownMenu<String?>(
dropdownMenuEntries: [
DropdownMenuEntry(value: null, label: 'Label'),
DropdownMenuEntry(value: '1', label: '1'),
DropdownMenuEntry(value: '2', label: '2'),
],
),
),
);
}
}
|
justinmc
left a comment
There was a problem hiding this comment.
LGTM 👍 . Also tagged @LongCatIsLooong for review since he wrote the issue.
Is it considered a breaking change?
See https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#1-determine-if-your-change-is-a-breaking-change. It certainly could break customers, but it looks like the customer tests are passing at least. Let's see if it passes Google tests too.
Is this PR text-exempted?
I can't think of any way to test this. Can you request an exemption? See that bot's comment above.
LongCatIsLooong
left a comment
There was a problem hiding this comment.
LGTM, thanks for the fix! This will need an exemption from discord. Since the ci is passing this is not considered breaking.
| /// selection was cleared / that no item was chosen. | ||
| /// | ||
| /// Defaults to null. If this callback itself is null, the widget still updates | ||
| /// the text field with the selected label but does not notify the caller. |
There was a problem hiding this comment.
Nit: remove the part after "but" since that sounds obvious and it's unclear who the "caller" is
There was a problem hiding this comment.
Sure, I removed it in Remove extra sentence
|
#176753 something to consider before this gets merged. |
|
test-exempt: code refactor with no semantic change |
|
@ValentinVignal my bad, I assumed this was changing the |
Manual roll Flutter from e11e2c1 to 7cd821c (73 revisions) Manual roll requested by [email protected] flutter/flutter@e11e2c1...7cd821c 2025-10-14 [email protected] Fix computeDistanceToActualBaseline throws when accessing child size (flutter/flutter#176906) 2025-10-14 [email protected] iOS can set application locale before view controller is set (flutter/flutter#176592) 2025-10-14 [email protected] Roll ANGLE to a branch based on d9fa255a5c22 (flutter/flutter#176747) 2025-10-14 [email protected] Relands "Fixes keyboard selects disabled radio" (flutter/flutter#176977) 2025-10-14 [email protected] Fix expansion tile is missing state announcement on non-Apple platforms (flutter/flutter#175480) 2025-10-14 [email protected] impeller: allows access of float uniforms by name (flutter/flutter#176728) 2025-10-14 [email protected] Roll dart sdk to 3.11.0-17.0.dev (flutter/flutter#176947) 2025-10-13 [email protected] Move iOS integration tests (flutter/flutter#176940) 2025-10-13 [email protected] Make sure that an InputDatePickerFormField doesn't crash in 0x0 envir… (flutter/flutter#176047) 2025-10-13 [email protected] [web] Match the behavior of other platforms in Web Locale.toString if the country code is an empty string (flutter/flutter#176862) 2025-10-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Fixes keyboard selects disabled radio (#176727)" (flutter/flutter#176958) 2025-10-13 [email protected] Fixes keyboard selects disabled radio (flutter/flutter#176727) 2025-10-13 [email protected] Roll Packages from e319c40 to d062181 (2 revisions) (flutter/flutter#176916) 2025-10-13 [email protected] Roll SwiftShader to 794b0cfce1d8 (flutter/flutter#176806) 2025-10-13 [email protected] Make DropdownMenu generic type non nullable (flutter/flutter#176711) 2025-10-12 [email protected] Roll Fuchsia Linux SDK from BWGpRvpdQh-HJpq1c... to _dd0Jv50H0oUI2Ad8... (flutter/flutter#176895) 2025-10-11 [email protected] Roll Fuchsia Linux SDK from JpiUsek1hU5r9QVKP... to BWGpRvpdQh-HJpq1c... (flutter/flutter#176880) 2025-10-11 [email protected] fix: content hash check for LUCI_CONTEXT (flutter/flutter#176867) 2025-10-11 [email protected] Feat: make tooltip position customizeable (flutter/flutter#175047) 2025-10-11 [email protected] Roll Dart SDK from d88d8bf2b53c to 65b171958c93 (3 revisions) (flutter/flutter#176871) 2025-10-11 [email protected] feat: apply radioGroup role to segmented control widgets (flutter/flutter#176157) 2025-10-10 [email protected] Make sure that a CheckboxMenuButton doesn't crash in 0x0 environment (flutter/flutter#176450) 2025-10-10 [email protected] [WebParagraph] Support for more styles, placeholders, decorations, etc (flutter/flutter#172853) 2025-10-10 [email protected] Set up a version of build_ios_framework_module_test that only runs on x64 machines and extend its timeout (flutter/flutter#176811) 2025-10-10 [email protected] Roll Packages from 0b41de3 to e319c40 (1 revision) (flutter/flutter#176833) 2025-10-10 [email protected] [tool/dap] Forward app.warning events from Flutter to DAP client (flutter/flutter#176827) 2025-10-10 [email protected] Roll Dart SDK from 70c00d3ceb3a to d88d8bf2b53c (1 revision) (flutter/flutter#176830) 2025-10-10 [email protected] Remove unnecessary nullable types in examples. (flutter/flutter#176713) 2025-10-10 [email protected] Roll Fuchsia Linux SDK from xArtL4DH0FsdwSqG_... to JpiUsek1hU5r9QVKP... (flutter/flutter#176822) 2025-10-10 [email protected] Cleanup OutlinedButton.icon documentation and implementation (flutter/flutter#176630) 2025-10-10 [email protected] [HCPP] Properly remove hcpp views that are no longer visible (flutter/flutter#176742) 2025-10-10 [email protected] Make sure that an InputChip doesn't crash in 0x0 environment (flutter/flutter#175930) 2025-10-10 [email protected] Update Flutter templates' Dart style (flutter/flutter#175963) 2025-10-10 [email protected] Make sure that a DropdownButtonFormField doesn't crash in 0x0 environ… (flutter/flutter#174958) 2025-10-10 [email protected] Make sure that an InkWell doesn't crash in 0x0 environment (flutter/flutter#175871) 2025-10-10 [email protected] Handle#6537 end drawer button (flutter/flutter#173026) 2025-10-10 [email protected] Roll Dart SDK from a9b7bd4b0b32 to 70c00d3ceb3a (4 revisions) (flutter/flutter#176815) 2025-10-10 [email protected] Change default Linux thread policy to merge platform and UI threads. (flutter/flutter#176759) 2025-10-09 [email protected] [ Tool ] Roll package:dwds to 26.0.0 (flutter/flutter#176808) 2025-10-09 [email protected] Update `CHANGELOG` to include 3.35.6 notes (flutter/flutter#176803) 2025-10-09 [email protected] Announce text and button together when DropdownMenu is treated as a button (flutter/flutter#176428) 2025-10-09 [email protected] [native_assets] create macOS CCompilerConfig via xcrun --find (flutter/flutter#175717) 2025-10-09 [email protected] [Impeller] Fix broken links in README. (flutter/flutter#176770) 2025-10-09 [email protected] Fix links to Custom Flutter Engine Embedders in README. (flutter/flutter#175807) ...
|
Hey @ValentinVignal 👋 Just found that there migt be a valid use case for allowing item's value to be null. Code sampleimport 'package:flutter/material.dart';
void main() {
runApp(const ReproApp());
}
class ReproApp extends StatelessWidget {
const ReproApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(title: 'DropdownMenu reset option', home: HomePage());
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return const Material(
child: Center(
child: DropdownMenu<String?>(
initialSelection: 500,
dropdownMenuEntries: const [
DropdownMenuEntry(value: null, label: ""),
DropdownMenuEntry(value: "value1", label: "Option 1"),
DropdownMenuEntry(value: "value2", label: "Option 2"),
DropdownMenuEntry(value: "value3", label: "Option 3"),
],
requestFocusOnTap: false,
textInputAction: TextInputAction.next,
label: Text('DropdownMenu'),
inputDecorationTheme: InputDecorationTheme(
floatingLabelBehavior: FloatingLabelBehavior.always,
border: OutlineInputBorder(),
),
),
),
);
}
}
Screen.Recording.2025-11-05.at.09.50.48.mov |
|
Oh no... @justinmc @LongCatIsLooong do we want to revert this PR then and only update the documentation to explain what |
|
The type constraints on the class should stay because |
|
I was using Can someone explain why |
|
Hi @JaffaKetchup, The reasoning behind reserving When
There's no way to distinguish between these two scenarios, which is why the constraint was added. That said, I understand your use case, you likely wanted a "clear selection" option in the dropdown itself. As a workaround, you could use a sentinel value approach: class Value<T> {
const Value(this.value);
final T? value;
}And change DropdownMenu<String?>(
dropdownMenuEntries: const [
DropdownMenuEntry(value: null, label: ""),
DropdownMenuEntry(value: "value1", label: "Option 1"),
DropdownMenuEntry(value: "value2", label: "Option 2"),
],
),into DropdownMenu<Value<String>>(
dropdownMenuEntries: const [
DropdownMenuEntry(value: Value(null), label: ""),
DropdownMenuEntry(value: Value("value1"), label: "Option 1"),
DropdownMenuEntry(value: Value("value2"), label: "Option 2"),
],
), |
Fixes 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
|
@ValentinVignal this is clearly breaking. I would reconsider the approach. Even if the pipline didn't fail for it, I suspect many are affected by this change. We use the null value in all our products. I agree that the Off topic: limiting the conversation of the issues to collaborators, before the feature was released to stable, just unnecessarly shifts the conversation to the PRs. Sometimes its better discussing with the previous context instead of having to create a new issue (e.g. also for commenters who want to provide a solution to a locked issue). |
The approach could be to explicitly accept null values for dropdown menu entries. The main change would be: final List<DropdownMenuEntry<T?>> dropdownMenuEntries;This would restore the previous behavior and make it clear that an entry with a null value is valid. |
As explained in #180121 the PR #176711 is a breaking change, which must be properly documented. Closes #180121 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## 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. - [ ] 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. - [ ] 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
This PR cherrypicks #181074 into beta, which is mostly a revert of #176711 (except that some docs are left unreverted and a few new tests are added.) This is needed to address a breakage as described in #180121. **Impacted Users:** Apps that use `DropdownMenu` or `DropdownMenuFormField` for a nullable class, such as `DropdownMenu<int?>`. **Impact Description:** Can not compile. **Workaround:** Yes, through non-trivial changes. For example, the developer can replace `DropdownMenu<int?>` with `DropdownMenu<Maybe<int>>`, as long as they import `Maybe` or create their own equivalence. **Risk:** Low. This PR is basically a revert. **Test Coverage:** Yes. The widget has high test coverage. **Validation Steps:** The PR compiles and tests pass.
As explained in flutter#180121 the PR flutter#176711 is a breaking change, which must be properly documented. Closes flutter#180121 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## 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. - [ ] 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. - [ ] 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
Fixes #175090
Pre-launch Checklist
///).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. Comments from the
gemini-code-assistbot 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.