Search code examples
flutterdio

Error: Bad state: Future already completed


enter image description here

I tried to call api using dio then I met this error.

` Future _getHomeData() async { try { var homeDataResponse = await HomeService().getHomeData();

  final classList = (homeDataResponse.data['classes']) as List;
  classes = classList.map((i) => ClassModel.fromJson(i)).toList();
  _storageService
      .writeSecureData(StorageItem("user_classes", jsonEncode(classes)));

  final examList = (homeDataResponse.data['exams']) as List;
  exams = examList.map((i) => Exam.fromJson(i)).toList();
  _storageService
      .writeSecureData(StorageItem("user_exams", jsonEncode(exams)));

  final taskList = (homeDataResponse.data['tasks']) as List;
  tasks = taskList.map((i) => Task.fromJson(i)).toList();
  _storageService
      .writeSecureData(StorageItem("user_tasks", jsonEncode(exams)));

  // for (var subject in subjects) {
  //   print("SUBJECtS ${subject.subjectName}");
  // }
} catch (error) {
  if (error is DioError) {
    throw Result.error(error.response?.data['message']);
  } else {
    throw Result.error(error.toString());
  }
}

} `


Solution

  • Since you wrote as a comment that you use version 4.0.6, I can inform you that version had a known issue. It is solved in a later version, so update to at least 5.0 of dio and all will be good.

    You can read about the issue here and read about it's fix here.