flutter-在ListView中进行API调用

发布于 2025-01-30 14:12:46 字数 829 浏览 3 评论 0原文

我正在调用API,该API将返回项目列表(锻炼),并且项目将在listView中显示。但是,我还需要为ListView的每个项目致电另一个API,以获取该锻炼的分数(如果已经有分数),并根据其ID向每个锻炼显示分数。如何解决这种情况?我正在使用Bloc,这就是我如何进行锻炼的方式,

Future<void> _onLoadingWod(
      OnLoadingWodEvent event, Emitter<WhiteboardState> emit) async {
    emit(state.copyWith(isWodLoading: true));
    final listWods =
        await WhiteboardService().getListWod(state.selectedDay.toString());

    emit(state.copyWith(
        isWodLoading: false, listWods: listWods));
  } 

某些锻炼可能还没有分数,在这种情况下,我在ListView中显示一个按钮来记录每个锻炼的分数。

我尝试了这样的事情:

    final listResults = await Stream.fromIterable(listWods)
        .asyncMap((wod) => WhiteboardService().getresult(wod.id))
        .toList();

但是由于某些锻炼可能没有结果,因此显示出错误。

如何通过结果进行链接锻炼并在listView中显示?请感谢任何帮助指南。

I am calling an api, which will return a list of items (workouts), and items will be shown in a ListView. but I also need to call another api for each item of the ListView to fetch the score of that workout (if there is already a score) and show the score to each workout according to their id. How can I resolve this scenario.? I am using bloc and this is how I am fetching the workouts

Future<void> _onLoadingWod(
      OnLoadingWodEvent event, Emitter<WhiteboardState> emit) async {
    emit(state.copyWith(isWodLoading: true));
    final listWods =
        await WhiteboardService().getListWod(state.selectedDay.toString());

    emit(state.copyWith(
        isWodLoading: false, listWods: listWods));
  } 

some workouts may not have a score yet, in that case, I show in the listview a button to log a score for each workout.

I tried something like this:

    final listResults = await Stream.fromIterable(listWods)
        .asyncMap((wod) => WhiteboardService().getresult(wod.id))
        .toList();

but since some workouts may not have result, is showing error.

how can a link workouts with results and show then in a listview? pls any help o guide is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Spring初心 2025-02-06 14:12:46

据我了解,可以从循环中完成。您必须为包括锻炼ID的结果创建模型类。然后,您可以循环浏览它,并从锻炼清单中找到相关的锻炼,然后为其分配结果。

有一个例子希望它会有所帮助。

*虚拟模型类

class Workouts {
  final int id;
  Result? result;

  Workouts({
    required this.id,
    this.result,
  });
}

class Result {
  final int workoutId;
  final String result;

  Result({
    required this.workoutId,
    required this.result,
  });
}

*将结果分配给锻炼清单,

void asignResults() {
    ///for each [results]
    for (Result result in listResults) {
      listWorkouts
          .firstWhere((workout) => workout.id == result.workoutId)
          .result = result;
    }
    listWorkouts.forEach((element) {
      print(element.toString());
    });
  }

我认为如果您可以从后端做到这一点,它将比这种方法更有效。 (例如: - 在SQL中加入查询)

As I understand, it can be done from a loop. You have to create a model class for results that include the workout id. Then you can loop through it and find the relevant workout from the workout list and then assign the results for it.

there is an example hope it will help.

*dummy model classes

class Workouts {
  final int id;
  Result? result;

  Workouts({
    required this.id,
    this.result,
  });
}

class Result {
  final int workoutId;
  final String result;

  Result({
    required this.workoutId,
    required this.result,
  });
}

*assign results to workout list

void asignResults() {
    ///for each [results]
    for (Result result in listResults) {
      listWorkouts
          .firstWhere((workout) => workout.id == result.workoutId)
          .result = result;
    }
    listWorkouts.forEach((element) {
      print(element.toString());
    });
  }

I think if you can do this from the backend it will be more efficient than this approach. (eg:- join query in sql)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文