mApiRetry<T> function

Future<T?> mApiRetry<T>(
  1. Future<T> req(
    1. bool last
    )
)

Test code:

Implementation

// void main() async {
//   test("Test the tryAgain function", () async {
//     final int? videoCallResult = await mApiRetry<int>(getIntFuture);
//     if (videoCallResult == null) {
//       return;
//     }
//     print("videoCallResult::$videoCallResult");
//   });
// }
//
// Future<int> getIntFuture() async {
//   print("To getIntFuture get value start");
//   await Future.delayed(Duration(seconds: 1));
//   final nextInt = math.Random().nextInt(100);
//   if (nextInt.isEven) {
//     throw Exception("Error::1");
//   }
//   final millisecond = DateTime.now().millisecond;
//   return millisecond;
// }
///
Future<T?> mApiRetry<T>(Future<T> Function(bool last) req) async {
  const int maxRetries = 3;
  const Duration retryDelay = Duration(milliseconds: 600);
  int retryCount = 0;

  while (retryCount < maxRetries) {
    mLogger.d("Start get mApiRetry()::${T.toString()}::Start🚗.");
    try {
      final value = await req(retryCount >= maxRetries);
      if (value != null) {
        mLogger.d("Start get mApiRetry()::${T.toString()}::Done✅.");
        return value;
      }
      // break;
    } catch (e, s) {
      mLogger.e('mApiRetry::${T.toString()}::Error::$e\n',
          stackTrace: s, error: e);
      retryCount++;
      if (retryCount >= maxRetries) {
        mLogger.e('Max retries reached. Handling error after final attempt.');
        // Handle error after final attempt here
        // break;
        return null;
      } else {
        await Future.delayed(retryDelay);
      }
    }
  }
  return null;
}