如何将一个 Future 的结果传递给另一个 Future?
How to pass the result of a Future to another Future?
如何将 Future convertGroup 的结果传递给 Future getList() 以获取列表中的结果?
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;
}
}
您只需在 getList
方法中 await convertGroup()
即可。例如:
Future<List<Group>> getList(Group group) async {
final List<Group> groups = [];
final groupWithId = await convertGroup(group);
groups.add(groupWithId);
return groups;
}
如何将 Future convertGroup 的结果传递给 Future getList() 以获取列表中的结果?
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;
}
}
您只需在 getList
方法中 await convertGroup()
即可。例如:
Future<List<Group>> getList(Group group) async {
final List<Group> groups = [];
final groupWithId = await convertGroup(group);
groups.add(groupWithId);
return groups;
}