Home > database >  How to pass the result of a Future to another Future?
How to pass the result of a Future to another Future?

Time:06-05

How can I pass the result of Future convertGroup to Future getList() to get the result in a list?

class GroupRepository extends BaseRepository<GroupDao, GroupDBData, Group> {
      @override
      GroupDao dao = injector<AppDb>().groupDao;
      @override
      BaseConverter<GroupDBData, Group> converter = GroupDbConverter();
      Future<Group> convertGroup(Group group) async {
        final convert = converter.outToIn(group);
        final groupId = await dao.insert(convert);
        Group groupWithID = Group(
          id: groupId,
          groupName: group.groupName,
        );
      
        return groupWithID;
      }

      Future<List<Group>> getList(Group group) async {
        final List<Group> groups = [];
        groups.add(groupWithID);
        
        return groups;
      }
    }

CodePudding user response:

All you need to do is await convertGroup() within the getList method. For example:

Future<List<Group>> getList(Group group) async {
  final List<Group> groups = [];
  final groupWithId = await convertGroup(group);

  groups.add(groupWithId);

  return groups;
}
  • Related