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
71 stars 58 forks source link

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

Open davidpanic opened 3 years ago

davidpanic commented 3 years ago
════════ 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.

diegopereira99 commented 3 years ago

How did u fix it?

bssughosh commented 3 years ago

Any updates here?

diegopereira99 commented 3 years ago

Not yet :(

davidpanic commented 3 years ago

How did u fix it?

I didn't

hitshydev commented 3 years ago

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.

alejandrocarlos commented 3 years ago

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

mipa1304 commented 3 years ago

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

Tetr4 commented 2 years ago

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>(),
);
TechHelper commented 2 years ago

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.

ris-tlp commented 2 years ago

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?

diaodiallo commented 1 year ago

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.

DyoungDsea commented 1 year ago

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