Skip to content

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