Home > front end >  Why this loop happens only once? Flutter/Dart
Why this loop happens only once? Flutter/Dart

Time:10-17

Hello there i have a 'while loop' which happens only one time and i dont know why,it suppose to happen 12 times but it didnt. Why???

Here is my code:

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');
}

CodePudding user response:

This is happened because you are calling return inside loop so at the first round when it gets return, it stops looping. try this:

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
    
  }
  • Related