retry method

Future<T> retry({
  1. int times = 3,
  2. Duration delay = const Duration(seconds: 1),
})

Implementation

Future<T> retry({
  int times = 3,
  Duration delay = const Duration(seconds: 1),
}) async {
  for (int i = 0; i < times; i++) {
    try {
      return await this;
    } catch (e) {
      print(e);
      if (i == times - 1) rethrow; // son denemede hata fırlat
      await Future.delayed(delay, () {});
      // tekrar denemeden önce bekle
    }
  }
  return await this; // bu satır pratikte ulaşılmaz
}