Flutter循环执行指定次数直至成功

Future<bool> loopRequest({final loopCount = 5}) async {
  int count = 0;
  bool b = false;
  await Future.doWhile(() async {
    ++count;
    try {
      bool result = await Future.delayed(Duration(seconds: 3),()=>false).timeout(Duration(seconds: 2));
      if (result) {
        b = true;
        return false;
      }
    } catch(e) {
      print('error $e');
    }
    return count < loopCount;
  });
  return b;
}

 

posted @ 2025-07-02 16:20  呢哇哦比较  阅读(5)  评论(0)    收藏  举报