Initial values showing issue
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
Having the same issue, no solution so far.
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),
));
});
}
- Please use correctly formated code blocks when opening issues
- Your code snipped shows nothing about what kind of data you recieve from
dataSource.singleWhere() - 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
@DavidCorrado thanks man
@DavidCorrado thanks man
@vishalpatelbacancy Poor DavidCorrado will be very confused why it is you thanked him xD
@DavidCorrado thanks man
@vishalpatelbacancy Poor DavidCorrado will be very confused why it is you thanked him xD
Sorry my bad, @david-kalbermatten thanks man :-)
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), )); }); }
- Please use correctly formated code blocks when opening issues
- Your code snipped shows nothing about what kind of data you recieve from
dataSource.singleWhere()- 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
valueFieldandtextFieldare of the typeStringyou have to provide aList<String>forinitialValue.So in your case @yuri4031, you have to provide a
List<int>forinitalValue
I encourage the maintainers to add key property to the widget
@redbayoub thanks mate, KeyedSubtree did the magic for me
happy to hear that man. happy coding.
On Tue, Nov 24, 2020 at 4:28 PM Rouvo [email protected] 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: [email protected]