【问题标题】:in Flutter, make a list of api call inside one api call在 Flutter 中,在一个 api 调用中列出 api 调用
【发布时间】: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) 来获取每个项目的描述并在该列表的每个项目中显示描述。

那么推荐的方法是什么。 提前致谢

【问题讨论】:

    标签: flutter api


    【解决方案1】:

    我不确定列表的显示方式,我猜您正在寻找 StreamasyncMap()

    这是一个示例实现,它将为您提供CourseProgressAndPerformance 的列表,这是我要研究的方向。

    var perfList = Stream
     .fromIterable(listOfCourses)
     .asyncMap((course) => getProgressAndPerformanceAnalysisOf(courseId))
     .toList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2021-02-09
      • 1970-01-01
      • 2017-11-22
      • 2021-02-13
      • 2021-01-24
      相关资源
      最近更新 更多