multiselect_formfield icon indicating copy to clipboard operation
multiselect_formfield copied to clipboard

type '() => Null' is not a subtype of type '(() => Map<String, dynamic>)?' of 'orElse'

Open davidpanic opened this issue 3 years ago • 12 comments

════════ 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.

davidpanic avatar Apr 29 '21 14:04 davidpanic

How did u fix it?

diegopereira99 avatar May 17 '21 19:05 diegopereira99

Any updates here?

bssughosh avatar May 19 '21 15:05 bssughosh

Not yet :(

diegopereira99 avatar May 19 '21 15:05 diegopereira99

How did u fix it?

I didn't

davidpanic avatar May 19 '21 20:05 davidpanic

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.

hitshydev avatar Jun 02 '21 12:06 hitshydev

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));

alejandrocarlos avatar Jul 13 '21 22:07 alejandrocarlos

issue will be sorted out with this link @hitshydev Thank You So much.

mipa1304 avatar Aug 02 '21 07:08 mipa1304

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>(),
);

Tetr4 avatar Nov 10 '21 13:11 Tetr4

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.

TechHelper avatar Dec 31 '21 18:12 TechHelper

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?

ris-tlp avatar May 14 '22 12:05 ris-tlp

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.

diaodiallo avatar Jun 02 '23 11:06 diaodiallo

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>>();

DyoungDsea avatar Sep 19 '23 07:09 DyoungDsea