cetorres / multiselect_formfield

A Flutter package that provides a multi select form field using alert dialog to select multiple items with checkboxes and showing as chips.
BSD 3-Clause "New" or "Revised" License
72 stars 59 forks source link

How can i clear/remove old value when change to new multiselect formfield #32

Open Thaimay opened 3 years ago

Thaimay commented 3 years ago

Here my follow. Thanks

  1. image
  2. image
  3. image
  4. image <--- because new multiselect formfield dont have selected item.
  5. image

@cetorres @DavidCorrado @SurajLad

ayinloya commented 3 years ago

I'm also getting this issue, Any solution yet?

The error occurs when the selected values are not present in the new datasource

How to reproduce

  1. Set datasource and an initial value
  2. Change the datasource to one that does not contain any element from the initial value

I guess the error comes from here

    state.value.forEach((item) {
            var existingItem = dataSource.singleWhere((itm) => itm[valueField] == item, orElse: () => null); // <-- this returns null when the item is not in the datasource
             selectedOptions.add(Chip(
                    labelStyle: chipLabelStyle,
                    backgroundColor: chipBackGroundColor,
                    label: Text(
                      existingItem[textField],  // <-- this calls [] on a null object
                      overflow: TextOverflow.ellipsis,
                      // style: TextStyle(color: Colors.red),
                    ),
            ));
    });

Selected elements can be ignored

    state.value.forEach((item) {
            var existingItem = dataSource.singleWhere((itm) => itm[valueField] == item, orElse: () => null); 
             // there could be a null check here to ignore value not present in new datasource
             if(existingItem != null){
                 selectedOptions.add(Chip(
                     labelStyle: chipLabelStyle,
                    backgroundColor: chipBackGroundColor,
                    label: Text(
                      existingItem[textField],
                      overflow: TextOverflow.ellipsis,
                      // style: TextStyle(color: Colors.red),
                    ),
               ));
          }
    });

If it's ok I can make a pull request.

rsree1210 commented 3 years ago

I am also facing the same issue. Any update on when the package will fix this issue? Or can anyone suggest any alternative solution to clear the field?

hampsterx commented 3 years ago

very temp fix, use my fork :(

https://github.com/cetorres/multiselect_formfield/compare/master...hampsterx:issue_32_clear_old_value_bug

this add a key and also avoids null bug here.

multiselect_formfield:
    # https://github.com/cetorres/multiselect_formfield/issues/32
    git:
      url: git://github.com/hampsterx/multiselect_formfield.git
      ref: issue_32_clear_old_value_bug

Didn't submit PR as wasn't sure if this is right fix or not. Works ok tho..