Skip to content

Improve DropdownButton assert message#43511

Merged
shihaohong merged 3 commits intoflutter:masterfrom
shihaohong:dropdown-duplicate-assert
Oct 28, 2019
Merged

Improve DropdownButton assert message#43511
shihaohong merged 3 commits intoflutter:masterfrom
shihaohong:dropdown-duplicate-assert

Conversation

@shihaohong
Copy link
Contributor

@shihaohong shihaohong commented Oct 25, 2019

Adds a helpful assert message to DropdownButton for when:

  • there are no items that match the passed in value for DropdownButton
  • there are duplicate item values

Related Issues

Fixes #43333

Tests

I added the following tests:

  • Test DropdownButton and DropdownButtonFormField for more helpful assertion messages if DropdownButton.value is not found in any of its items, or if duplicate items are found.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require Flutter developers to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (Please read Handling breaking changes). Replace this with a link to the e-mail where you asked for input on this proposed change.
  • No, this is not a breaking change.

@shihaohong shihaohong added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Oct 25, 2019
@shihaohong shihaohong requested a review from HansMuller October 25, 2019 17:17
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1,
'There should be one item with [DropdownButton]\'s value: $value. \n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be one => should be exactly one

return item.value == value;
}).length == 1,
'There should be one item with [DropdownButton]\'s value: $value. \n'
'There might be no [DropdownButtonItem]s with this value, or '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is stated in the same way that the expected (correct) configuration is stated in the first sentence. Might be better to say something like: "Either zero or 2 or more [DropdownMenuItem]s ..."

}
});

testWidgets('DropdownButton does not item list without given value', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DropdownButton value should only appear in one menu item

}
});

testWidgets('DropdownButtonFormField does not item list without given value', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about the test title as earlier

@shihaohong shihaohong merged commit 036c102 into flutter:master Oct 28, 2019
@shihaohong shihaohong deleted the dropdown-duplicate-assert branch October 28, 2019 20:44
Inconnu08 pushed a commit to Inconnu08/flutter that referenced this pull request Nov 26, 2019
- Adds an assert message checking for exactly one unique value for `DropdownItem`s
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding Duplicate items to the Dropdown widget fails to provide helpful error message.

3 participants