как поймать SocketException, используя http в дартс? - PullRequest
0 голосов
/ 01 апреля 2020

я пытаюсь поймать исключение SocketException в функции HttpResponse и вернуть диалоговое окно, но ошибка появляется до того, как она поступит туда. Нужна помощь, спасибо.

Future<Map<String, dynamic>> post(
  {@required context, @required String link, @required body, @required bool hasToken}) async {
http.Response response = await httpResponse(
    context: context,
    link: (await client.post(httpLink + link,
        body: body, headers: hasToken ? header(userProvider(context).token) : null)));
return response != null ? jsonValue(context: context, response: response) : null;
}

Future<http.Response> httpResponse({@required context, @required http.Response link}) async {
http.Response response;
try {
  response = link;
  print(link);
} catch (Exception) {
  await Dialogs.displayDialog(
      context: context,
      title: "Error",
      description: "Connection Error",
      alertType: AlertType.Error,
      confirm: () => Navigator.of(context).pop(true));
  homeServiceProvider(context).loadCancel();
  return null;
}
return response;
}

enter image description here

...