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

Initial values showing issue #22

Open
yuri4031 opened this issue Aug 26, 2020 · 8 comments
Open

Initial values showing issue #22

yuri4031 opened this issue Aug 26, 2020 · 8 comments

Comments

@yuri4031
Copy link

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
Copy link

colby2 commented Sep 29, 2020

Having the same issue, no solution so far.

@david-kalbermatten
Copy link

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
Copy link

vishalpatelbacancy commented Oct 28, 2020

@DavidCorrado thanks man

@david-kalbermatten
Copy link

david-kalbermatten commented Oct 28, 2020

@DavidCorrado thanks man

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

@vishalpatelbacancy
Copy link

@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
Copy link

redbayoub commented Nov 11, 2020

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
Copy link

Rouvo commented Nov 24, 2020

@redbayoub thanks mate, KeyedSubtree did the magic for me

@vishalpatelbacancy
Copy link

vishalpatelbacancy commented Dec 1, 2020 via email

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

6 participants