Christian
Christian

Reputation: 26437

Turning List<MapEntry<int, bool>> into Map<int, bool> in dart

I want to create a map with maps the numbers from 1 to 10 to count values. Currently, I do:

Iterable<int> spread = Iterable<int>.generate(10);
Iterable<Future<MapEntry<int, bool>>> futureEntries= spread.map((i) async {
  int level = i + 1;
  bool levelRight = await checkCountForLevel(level);
  return MapEntry(level, levelRight);
});
List<MapEntry<int, bool>> listOfEntries = await Future.wait(futureEntries);
Map<int, bool> levelCorrect = Map.fromIterable(listOfEntries);

However, I get the error:

E/flutter (29690): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'MapEntry<int, bool>' is not a subtype of type 'int' of 'key'

Do I have to use a different method then Map.fromIterable?

Upvotes: 1

Views: 814

Answers (1)

julemand101
julemand101

Reputation: 31299

Before I am trying to fix your solution, I just want to know if you are aware that you could do this instead?

Future<void> main() async {
  final levelCorrect = {
    for (var level = 1; level <= 10; level++)
      level : await checkCountForLevel(level)
  };
}

If you want to use your own solution, you should use Map.fromEntries instead of Map.fromIterable.

Upvotes: 4

Related Questions