Skip to content
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

initialValue not work in MultiSelectChipField #11

Closed
msaber2015 opened this issue Dec 28, 2020 · 11 comments
Closed

initialValue not work in MultiSelectChipField #11

msaber2015 opened this issue Dec 28, 2020 · 11 comments

Comments

@msaber2015
Copy link

No description provided.

@John-King-456
Copy link

initialValue also not working in MultiSelectDialogField. Thanks.

@augb
Copy link

augb commented Jan 12, 2021

Could this be the reason?

Widget _buildInheritedChipDisplay() {
  List<MultiSelectItem<V>> chipDisplayItems = [];
  chipDisplayItems = _selectedItems
      .map((e) => widget.items
          .firstWhere((element) => e == element.value, orElse: () => null))
      .toList();
  chipDisplayItems.removeWhere((element) => element == null);

Could it be that support for a Comparable object is necessary?

@augb
Copy link

augb commented Jan 25, 2021

Getting back to looking at this, a simple solution that doesn't require change the library would be to override the == operator (and, therefore, also hashCode).

(I'm new to Dart/Flutter so this was not immediately obvious to me.)

@taufan-mft
Copy link

Thanks @augb for pointing me in the right direction. For anyone that might stumbled upon this, the solution is to override the == operator and hashCode just like augb said. For example:

class Person {
  final String name;

  const Person(this.name);

  @override
  bool operator ==(Object other) =>
    identical(this, other) ||
    other is Person &&
    runtimeType == other.runtimeType &&
    name == other.name;

  @override
  int get hashCode => name.hashCode;
}

@altafkhan8719
Copy link

i am also facing same issue,
even after overriding == operator

@CHB61
Copy link
Owner

CHB61 commented Feb 14, 2021

Hello, I'm trying to reproduce this issue. Can someone please provide a code example where this is failing?

I'm using version 3.1.6 and have tried a couple simple examples.

// setting the entire list as initial value
MultiSelectChipField(
    items: _items,
    initialValue: _animals,
    onSaved: (val) {
        _selectedAnimals = val;
    },
)
// setting a couple values from the list
MultiSelectDialogField(
    onConfirm: (val) {
        _selectedAnimals = val;
    },
    items: _items,
    initialValue: [_animals[0], _animals[2]],
),
// setting the _selectedAnimals3 to desired pre-selected values list in initState - recommended
MultiSelectBottomSheetField(
    onConfirm: (values) {
        _selectedAnimals3 = values;
    },
    items: _items,
    initialValue: _selectedAnimals3,
    chipDisplay: MultiSelectChipDisplay(
        onTap: (item) {
            _selectedAnimals3.remove(item);
            return _selectedAnimals3;
        },
    ),
),

I've also tried running the example app with no issue.

@madhan-asmb
Copy link

Hello, I'm trying to reproduce this issue. Can someone please provide a code example where this is failing?

I'm using version 3.1.6 and have tried a couple simple examples.

// setting the entire list as initial value
MultiSelectChipField(
    items: _items,
    initialValue: _animals,
    onSaved: (val) {
        _selectedAnimals = val;
    },
)
// setting a couple values from the list
MultiSelectDialogField(
    onConfirm: (val) {
        _selectedAnimals = val;
    },
    items: _items,
    initialValue: [_animals[0], _animals[2]],
),
// setting the _selectedAnimals3 to desired pre-selected values list in initState - recommended
MultiSelectBottomSheetField(
    onConfirm: (values) {
        _selectedAnimals3 = values;
    },
    items: _items,
    initialValue: _selectedAnimals3,
    chipDisplay: MultiSelectChipDisplay(
        onTap: (item) {
            _selectedAnimals3.remove(item);
            return _selectedAnimals3;
        },
    ),
),

I've also tried running the example app with no issue.

3rd Code block cleared my issue (on tap of chipDisplay remove the taped initial value).

we need to add the return statement in the onTap function

Thanks, @CHB61

@CHB61
Copy link
Owner

CHB61 commented Mar 7, 2021

@madhan-asmb Great! I hope this solutions works for everyone experiencing this problem.

Going to drop a link to the first issue regarding this type of problem which provides more background.

Issue #5 Initial value not working as expected

@CHB61 CHB61 closed this as completed Mar 7, 2021
@ariefwijaya
Copy link

initialValue also not working in MultiSelectDialogField. Thanks.

@CHB61
Please open again this issue, initialValue Still not working for MultiSelectDialogField, can't be shown.

@MalikSamiAwan
Copy link

initialValue also not working in MultiSelectDialogField. Thanks.

@CHB61 Please open again this issue, initialValue Still not working for MultiSelectDialogField, can't be shown.

Did you find any solution?

@saifonglee
Copy link

Thank @augb and @taufan-mft .
it worked for me.

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

No branches or pull requests

10 participants