multiselect_formfield
multiselect_formfield copied to clipboard
type '() => Null' is not a subtype of type '(() => Map<String, dynamic>)?' of 'orElse'
════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building MultiSelectFormField(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#9fe74]], state: FormFieldState<dynamic>#113e5):
type '() => Null' is not a subtype of type '(() => Map<String, dynamic>)?' of 'orElse'
The relevant error-causing widget was
MultiSelectFormField
lib/…/select/multiselect.dart:41
When the exception was thrown, this was the stack
#0 ListMixin.singleWhere (dart:collection/list.dart)
#1 new MultiSelectFormField.<anonymous closure>._buildSelectedOptions.<anonymous closure>
package:multiselect_formfield/multiselect_formfield.dart:72
#2 List.forEach (dart:core-patch/growable_array.dart:403:8)
#3 new MultiSelectFormField.<anonymous closure>._buildSelectedOptions
package:multiselect_formfield/multiselect_formfield.dart:71
#4 new MultiSelectFormField.<anonymous closure>
package:multiselect_formfield/multiselect_formfield.dart:168
...
════════════════════════════════════════════════════════════════════════════════
Fix with this
This only happens when using SOUND null safety. It works fine with unsound null safety.
How did u fix it?
Any updates here?
Not yet :(
How did u fix it?
I didn't
I forked the project and changed the line, that's causing the error. When using dynamic, the type is only determined when running the app which seems to be not fully null-safe compatible.
You can use this in your pubspec.yml to ignore the error for now:
multiselect_formfield:
git:
url: https://github.com/hitshydev/multiselect_formfield
I would try to refactor this package to use generics which performs better with null-safety.
I was having this exact same issue, I did the same as @hitshydev except I just copied the class for the MultiSelectFormField. The line that's causing the issue is the following:
var existingItem = dataSource!.singleWhere(((itm) => itm[valueField] == item), orElse: () => null);
The orElse function is returning a null value which will trigger an error at run time since it won't be compatible with the type you provided in your dataSource.
Changing the line to the following will remove the error:
var existingItem = dataSource!.singleWhere(((itm) => itm[valueField] == item));
issue will be sorted out with this link @hitshydev Thank You So much.
An alternative workaround is to cast the dataSource to dynamic:
MultiSelectFormField(
dataSource: myItems
.map((item) => ({'value': item.id, 'text': item.name}))
.toList()
// see https://github.com/cetorres/multiselect_formfield/issues/38
.cast<dynamic>(),
);
I also faced same problem. The problem was data source List<Map<string, dynamic>> was having null values sometimes. I made it like this List<Map<String, dynamic>?> and it worked. I did this for dynamic data I am getting from API.
Had the exact same issue and was able to solve it using @Tetr4's suggestion. Will a fix for this be shipped out anytime soon?
I was having this issue and unfortunately I added another issue above. I was able to fix this by replacing () => null by null at line 72.
List<Map<String, dynamic>>? routesMapList = routes ?.map((route) => { 'value': route.rid as String, 'label': route.droute as String, }) .toList().cast<Map<String, dynamic>>();
this how I solved the problem .cast<Map<String, dynamic>>();