【发布时间】:2021-09-09 21:46:21
【问题描述】:
在我的一个flutter应用程序中,起初我想调用一个api,它将返回一个项目列表,该项目将显示在一个ListView中。我还需要为 ListView 的每个项目调用另一个 api 以获取该项目的描述并根据它们的 id 显示每个项目的描述。我该如何解决这种情况。在 RxJava 中,有一个称为 flatmap 的运算符,它可以毫不费力地做同样的事情。但是在颤振中,我该如何实现这一点。这是我的 2 个函数
class HomeRepositoryImpl extends HomeRepository {
HomeGraphQLService homeGraphQLService;
HomeMapper homeMapper;
HomeRepositoryImpl(HomeGraphQLService homeGraphQLService, HomeMapper homeMapper) {
this.homeGraphQLService = homeGraphQLService;
this.homeMapper = homeMapper;
}
@override
Future<List<Course>> getAllCourseOf(String className, String groupName) async {
final response = await homeGraphQLService.getAllCourseOf(className, groupName);
return homeMapper.toCourses(response).where((course) => course.isAvailable);
}
@override
Future<CourseProgressAndPerformance> getProgressAndPerformanceAnalysisOf(String subjectCode) async {
final response = await homeGraphQLService.getProgressAndPerformanceAnalysisOf(subjectCode);
return homeMapper.toProgressAndPerformance(response);
}
}
在上面的类中,首先我调用 getAllCourseOf() 函数来获取课程列表并在列表视图中显示它们。我需要调用 getProgressAndPerformanceAnalysisOf(courseId) 来获取每个项目的描述并在该列表的每个项目中显示描述。
那么推荐的方法是什么。 提前致谢
【问题讨论】: