How to make a loop for a function until a bool is true?

I have a function that gets some info from firebase storage using url,these info contains a bool, what I want to achieve is to loop for a true value in each nested object I have in this JSON file and stop when it found a true value.

Code:

Future getAya() async {
    await checkInternet();
    await down_data();
    if (khatma == 'Khatma 1') {
      var rng = new Random();
      int ranum = rng.nextInt(2);
      var response = await http.get(
        'firebase link',
      );
      var myDataString = utf8.decode(response.bodyBytes);
      data = jsonDecode(myDataString);
      if (state = true) {
        print('Taken');
      } else {
        setState(() {
          ranAya = data['verse_$ranum']['Aya'];
          state = data['verse_$ranum']['taken'];
          audioLink = data['verse_$ranum']['audioLink'];
        });
      }

1 answer

  • answered 2020-09-24 06:15 R.Shpd

    You can use do while loop like this:

     Future getAya() async {
      bool state = false;
    
      do{
        await checkInternet();
        await down_data();
        if (khatma == 'Khatma 1') {
          var rng = new Random();
          int ranum = rng.nextInt(2);
          var response = await http.get(
            'firebase link',
          );
          var myDataString = utf8.decode(response.bodyBytes);
          data = jsonDecode(myDataString);
          state = data['verse_$ranum']['taken'];
    
          if (state) {
            print('Taken');
          } else {
            setState(() {
              ranAya = data['verse_$ranum']['Aya'];
              audioLink = data['verse_$ranum']['audioLink'];
            });
          }
        }
      }while(!state);
    }