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

Initial values showing issue #22

Open yuri4031 opened 4 years ago

yuri4031 commented 4 years ago

Hi so I'm using this array as my datasource [{"id":2,"name":"Carpet Furniture Wash"}] and using textField: 'name', valueField: 'id',

if my initialvalues are empty the dialog is working fine and selected values are shown on ok click

but if i set my intialvalues just like the above data source then I'm getting error on these lines

if (state.value != null) { state.value.forEach((item) { var existingItem = dataSource.singleWhere((itm) => itm[valueField] == item, orElse: () => null); selectedOptions.add(Chip( label: Text(existingItem[textField], overflow: TextOverflow.ellipsis), )); }); }

the bold line has an issue where we are comparing a field with object, which i think should be compared with valueField of datasource item.

Can you please look into it? Here's my stacktrace:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ 2020-08-27 00:38:39.899 10319-10414/com.yallah.arbi I/flutter: The following NoSuchMethodError was thrown building MultiSelectFormField(dirty, dependencies: 2020-08-27 00:38:39.899 10319-10414/com.yallah.arbi I/flutter: [_FormScope], state: FormFieldState#a128f): 2020-08-27 00:38:39.899 10319-10414/com.yallah.arbi I/flutter: The method '[]' was called on null. 2020-08-27 00:38:39.899 10319-10414/com.yallah.arbi I/flutter: Receiver: null 2020-08-27 00:38:39.899 10319-10414/com.yallah.arbi I/flutter: Tried calling: 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: The relevant error-causing widget was: 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: MultiSelectFormField 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: file:///Volumes/Stuff/Dev/Mine/Flutter/yalla/lib/ui/provider/provider_profile.dart:415:12 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: When the exception was thrown, this was the stack: 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: #1 new MultiSelectFormField.._buildSelectedOptions. (package:multiselect_formfield/multiselect_formfield.dart:58:45) 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: #2 List.forEach (dart:core-patch/growable_array.dart:289:8) 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: #3 new MultiSelectFormField.._buildSelectedOptions (package:multiselect_formfield/multiselect_formfield.dart:55:29) 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: #4 new MultiSelectFormField. (package:multiselect_formfield/multiselect_formfield.dart:140:60) 2020-08-27 00:38:39.900 10319-10414/com.yallah.arbi I/flutter: #5 FormFieldState.build (package:flutter/src/widgets/form.dart:439:26)

colby2 commented 4 years ago

Having the same issue, no solution so far.

david-kalbermatten commented 4 years ago
if (state.value != null) {
  state.value.forEach((item) {
    var existingItem = dataSource.singleWhere((itm) => itm[valueField] == item, orElse: () => null);
    selectedOptions.add(Chip(
      label: Text(existingItem[textField], overflow: TextOverflow.ellipsis),
    ));
  });
}
  1. Please use correctly formated code blocks when opening issues
  2. Your code snipped shows nothing about what kind of data you recieve from dataSource.singleWhere()
  3. This seems to be a user error, as I have no issues setting initial values

Example

return MultiSelectFormField(
  title: Text('Barcode Formats'),
  dataSource: BarcodeFormat.values.map((barcodeFormat) {
    return {'display': barcodeFormat.name, 'value': barcodeFormat.value};
  }).toList(),
  textField: 'display',
  valueField: 'value',
  initialValue: List<String>.from(snapshot.data),
  onSaved: (values) => persistBarcodeSettings(values: values),
);

If both your valueField and textField are of the type String you have to provide a List<String> for initialValue.

So in your case @yuri4031, you have to provide a List<int> for initalValue

vishalpatelbacancy commented 4 years ago

@DavidCorrado thanks man

david-kalbermatten commented 4 years ago

@DavidCorrado thanks man

@vishalpatelbacancy Poor DavidCorrado will be very confused why it is you thanked him xD

vishalpatelbacancy commented 4 years ago

@DavidCorrado thanks man

@vishalpatelbacancy Poor DavidCorrado will be very confused why it is you thanked him xD

Sorry my bad, @david-kalbermatten thanks man :-)

redbayoub commented 4 years ago

I've had a similar problem and the my problem was intialValue comes after a future which is after building the widget so my solution is you force rebuilding the widget using a key but the widget doesn't have key property so i use KeyedSubtree Widget

 UniqueKey specialtiesSelectorKey=UniqueKey();

// after resolving the future use 
setState(() {
        specialtiesSelectorKey=UniqueKey();
});

  @override
  Widget build(BuildContext context) {
 return KeyedSubtree(
                            key: specialtiesSelectorKey,
                            child:  MultiSelectFormField(
                            autovalidate: false,
                            chipBackGroundColor: CustomColors.primaryColor,
                            chipLabelStyle:
                            TextStyle(fontWeight: FontWeight.bold),
                            dialogTextStyle: ..... );
}

and you must ensure that intialValue is list of the same type as valueField in dataSource as stated by @david-kalbermatten

if (state.value != null) {
  state.value.forEach((item) {
    var existingItem = dataSource.singleWhere((itm) => itm[valueField] == item, orElse: () => null);
    selectedOptions.add(Chip(
      label: Text(existingItem[textField], overflow: TextOverflow.ellipsis),
    ));
  });
}
  1. Please use correctly formated code blocks when opening issues
  2. Your code snipped shows nothing about what kind of data you recieve from dataSource.singleWhere()
  3. This seems to be a user error, as I have no issues setting initial values

Example

return MultiSelectFormField(
  title: Text('Barcode Formats'),
  dataSource: BarcodeFormat.values.map((barcodeFormat) {
    return {'display': barcodeFormat.name, 'value': barcodeFormat.value};
  }).toList(),
  textField: 'display',
  valueField: 'value',
  initialValue: List<String>.from(snapshot.data),
  onSaved: (values) => persistBarcodeSettings(values: values),
);

If both your valueField and textField are of the type String you have to provide a List<String> for initialValue.

So in your case @yuri4031, you have to provide a List<int> for initalValue

I encourage the maintainers to add key property to the widget

Rouvo commented 4 years ago

@redbayoub thanks mate, KeyedSubtree did the magic for me

vishalpatelbacancy commented 4 years ago

happy to hear that man. happy coding.

On Tue, Nov 24, 2020 at 4:28 PM Rouvo notifications@github.com wrote:

@redbayoub https://github.com/redbayoub thanks mate, KeyedSubtree did the magic for me

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cetorres/multiselect_formfield/issues/22#issuecomment-732823193, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGDF266ES6KWYUXRKVQQNN3SROGW7ANCNFSM4QMGCTHQ .

--

Vishal Patel - Android developer Bacancy Technology Pvt Ltd p: 079- 40299222 a: 201, Shukan Complex, Above Hind Super Market, Thaltej shilaj Road Thaltej Ahmedabad-380059 w: www.bacancytechnology.com e: vishalpatel@bacancytechnology.com