Asynchronous 确定执行者何时完成所有任务

Asynchronous 确定执行者何时完成所有任务,asynchronous,dart,concurrency,Asynchronous,Dart,Concurrency,我正在尝试编写一个dart函数,它将得到玩家的体重,并最终给出所有玩家的平均体重。 我正在使用executor软件包一次性获取3名玩家的体重,然后一旦获取一名玩家的体重,我就将其添加到列表中 我不能在for循环之前添加wait,也不能在for循环执行之后不添加wait代码。 我是否可以暂停程序或仅在执行器任务完成时返回值 avgWeight(int n)async { List playersWeight=[]; Executor executor = Executor(conc

我正在尝试编写一个dart函数,它将得到玩家的体重,并最终给出所有玩家的平均体重。 我正在使用executor软件包一次性获取3名玩家的体重,然后一旦获取一名玩家的体重,我就将其添加到列表中

我不能在for循环之前添加wait,也不能在for循环执行之后不添加wait代码。 我是否可以暂停程序或仅在执行器任务完成时返回值

avgWeight(int n)async {
    List playersWeight=[];
    Executor executor = Executor(concurrency: 3);
    for (int i = 0; i < n; i++) {
//      executor.join(withWaiting: true).;
      executor.scheduleTask(() async {
        int currentPlayerWeight = await PlayerDetail(i+1).fetchPlayerWeight();
        print('courrentPlayerNo: ${i+1} currentPlayerWeight : $currentPlayerWeight ');

        if(currentPlayerWeight!=null){
          await playersWeight.add(currentPlayerWeight);
        }

      });
     await  executor.join(withWaiting:true);
      print(playersWeight);//for debugging only. this shuld be printed only when all tasks are completed. 
    }
    //playersWeight.reduce((a, b) => a + b) / playersWeight.length
  }
所需产出:

[200, 190, 265, 255]

我需要返回平均重量,但由于当前问题,我无法返回。

如注释中所示,问题在于
等待执行器。加入(withWaiting:true)在for循环内,但应该在for循环外。

连接的原因是否在for循环内?我似乎没什么道理。如果这不是问题,那么你能举一个小例子来说明问题,并且可以由其他人执行吗?我现在觉得自己很愚蠢。在这件事上浪费了太多时间。就连我的朋友也没能发现这个错误。非常感谢。在把它搬出去之后,它现在可以工作了,没有问题。我添加了一个您可以接受的答案。:)
[200, 190, 265, 255]