你好,我有一个‘While循环’,它只发生一次,我不知道为什么,它应该发生12次,但它没有.为什么?

以下是我的代码:

Future<List<Evsebillall>> fetcheach() async {
    int i=1;
    while (i<12) {
      print(i);
      i++;
    String? token = await this.storage.read(key: "token");
    Map<String, String> headers = {
      "Content-Type": "application/json",
      "Accept": "application/json",
      "Authorization": "Bearer " + (token ?? ""),
    };
    final response = await http.get(Uri.parse(
        this.serverIP + ':' + this.serverPort +
            '/user/contractedChargeTransactions?month=$i&year=2022'),
        headers: headers);
    if (response.statusCode == 200) {
      setState(() {
        cardBillsTotaleach = jsonDecode(response.body)["cardBillsTotal"] as List;
        var resultTotal = cardBillsTotaleach.map((e) => Evsebill.fromJson(e)).toList();
        if(resultTotal.isNotEmpty) {
          TotalTotaleach = (resultTotal[0].total);
          print("totaleach =$TotalTotaleach");
          Totaleachlist.add(TotalTotaleach);
        }
        print ("listtotal =$Totaleachlist");
      });
      return cardBillsall.map((e) => Evsebillall.fromJson(e)).toList();
    }
    else{
      throw Exception('Failed to load Bills');
    }
  }
    throw Exception('Failed to load Bills');
}

推荐答案

这是因为您在循环内部调用了Return,所以在第一个循环中,当它得到Return时,它停止循环.试试这个:

Future<List<Evsebillall>> fetcheach() async {
    int i = 1;
    List<Evsebillall> result = []; // <--add this
    while (i < 12) {
      print(i);
      i++;
      String? token = await this.storage.read(key: "token");
      Map<String, String> headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": "Bearer " + (token ?? ""),
      };
      final response = await http.get(
          Uri.parse(this.serverIP +
              ':' +
              this.serverPort +
              '/user/contractedChargeTransactions?month=$i&year=2022'),
          headers: headers);
      if (response.statusCode == 200) {
        setState(() {
          cardBillsTotaleach =
              jsonDecode(response.body)["cardBillsTotal"] as List;
          var resultTotal =
              cardBillsTotaleach.map((e) => Evsebill.fromJson(e)).toList();
          if (resultTotal.isNotEmpty) {
            TotalTotaleach = (resultTotal[0].total);
            print("totaleach =$TotalTotaleach");
            Totaleachlist.add(TotalTotaleach);
          }
          print("listtotal =$Totaleachlist");
        });
        result.addAll(cardBillsall.map((e) => Evsebillall.fromJson(e)).toList());// <--add this
      } else {
        throw Exception('Failed to load Bills');
      }
    }

    return result;// <--add this
    
  }

Flutter相关问答推荐

未处理异常:字符串类型不是值类型子类型的子类型'

我如何才能收到每天重复的预定通知?

GetConnect Post API调用在Flutter Web中不起作用说:415不支持的媒体类型

为什么Spotify Auth API返回400,而";code_verier不正确?

Flutter :执行com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction时出现故障

在 flutter 吧蜂巢中拯救主题

如何在Flutter 中创造以下效果

使用Ffltter CREATE创建项目时,androidManifest.xml中缺少包属性

导航回上一页会将我带回Ffltter应用程序中的登录页面

dart /长笛中的返回早期模式和拆分方法

在 Flutter 中解码 BLE 设备负载

Flutter应用无法上传到Microsoft Store

如何在Flutter中动态绘制一条从A点到B点的线?

在 Flutter 中使用 HardwareKeyboard 时所有键都没有响应

出现错误;运算符[]不是为对象类型定义的?功能()'

我一直在try 创建按钮以导航到第二页,但不知何故 RaisedButton 功能无法正常工作

Positioned 的宽度或 SizedBox 的宽度都不适用于堆栈小部件 - Flutter

如何在Flutter 中根据其父小部件高度设置图标按钮飞溅半径

我已经删除了我的 .android 密钥库,但我没有使用它,如何生成一个新的?

Flutter Firebase 可以在真实设备上运行,而不是在 Android 模拟器上运行