Skip to content

Fix for duplicate values using setState with initially selected items #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mogelbuster
Copy link

Fix for duplicate values appearing for each call to setState when initializing MultiSelectContainer with pre-selected items. For example when initializing MultiSelectContainer like this:

MultiSelectContainer(
  controller: _controller,
  items: _myItems.map((item) => MultiSelectCard(value: item, label: item.name, selected: _mySelectedItems.any((s) => s.id == item.id))).toList(),
),

In the above example I am setting some of the initial _myItems to selected: true based on their existence in _mySelectedItems.

The reason the selected values were being duplicated is because didUpdateWidget would call _addInitiallySelectedItemsToSelectedList which would blindly add those items to _selectedItems. This is OK in initState because it is only called once when the widget is created. But it is a problem in didUpdateWidget because it is called whenever the widget is updated, thus adding the same items to _selectedItems again and again.

Fix for duplicate values appearing for each call to setState when initializing MultiSelectContainer with pre-selected items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant