Home > database >  Flutter Unhandled Exception: type 'Future<Group>' is not a subtype of type 'Gro
Flutter Unhandled Exception: type 'Future<Group>' is not a subtype of type 'Gro

Time:05-14

While working with Firebase Firestore, I am coming across the following error:

E/flutter ( 4477): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: type 'Future<Group>' is not a subtype of type 'Group', stack trace: #0      new List.from (dart:core-patch/array_patch.dart:41:5)
E/flutter ( 4477): #1      GroupService.getGroups (package:money_manager/core/services/group_service.dart:56:21)
E/flutter ( 4477): <asynchronous suspension>
E/flutter ( 4477): #2      GroupViewModel._getAllGroups (package:money_manager/core/view_models/group_viewmodel.dart:44:26)
E/flutter ( 4477): <asynchronous suspension>
E/flutter ( 4477):

Though I think I have made proper use of async-await wherever necessary, I am not being able to figure out this error. Relevant code:

in group-service.dart :

Future<List<Group>> getGroups() async {
    final userData = await getUserData();
    List<Group> groups = [];

    if (userData['groups'] != null) {
      List<String> groupIds = List.from(userData['groups'].map((e) => e['id']));
      groups = List.from(groupIds.map((e) async {
        return await getGroupFromId(e);
      }));
    }

    return groups;
  }

  Future<Group> getGroupFromId(String groupId) async {
    final groupData = await getGroupData(groupId);
    return Group.fromJson(groupData);
  }

  Future<Map<String, dynamic>> getGroupData(String groupId) async {
    DocumentReference groupDoc =
        FirebaseFirestore.instance.collection('groups').doc(groupId);
    final snapshot = await groupDoc.get();
    Map<String, dynamic> groupData = snapshot.data() as Map<String, dynamic>;
    return groupData;
  }

in group_viewmodel.dart:

List<Group> _userGroups = [];
 void _getAllGroups() async {
    List<Group> groups = await _groupService.getGroups();
    _userGroups = groups;
  }

CodePudding user response:

The problem is in the following line:

groups = List.from(groupIds.map((e) async {
   return await getGroupFromId(e);
}));

Even though you're using await before the return of the map() function, it will still return a Future. map() is a synchronous function, and it doesn't run the inner function asynchronously. Thus, it will return an Iterable<Future<Group>>, which fails to be converted into a List<Group> in the List.from() function.

There is a handy function that takes an iterable of futures and waits for each one of them, Future.wait(). Here's how your code will look like with it:

groups = List.from(await Future.wait(groupIds.map((e) async {
   return await getGroupFromId(e);
})));

And even better with tear-offs:

groups = List.from(await Future.wait(groupIds.map(getGroupFromId)));
  • Related