Home > Software engineering >  How to deal with HTTP connection timed out crashes in Flutter
How to deal with HTTP connection timed out crashes in Flutter

Time:01-16

So I have a method that uses the Flutter HTTP library and is responsible for calling HTTP requests to the server with code like this:

Future<List<DataModel>> fetchData() async {
  try {
    var url = Uri.parse('${baseUrlParse}myapipath');
    var request = await http.get(url);

    var data = jsonDecode(request.body);

    return data;
  } catch (e) {
    print('Catch ${e}');
    rethrow;
  }
}

This code runs fine and has no issues.
It got to the point where when I have no internet connection or server connection fails, the app freezes, and an error file appears (if you're debugging in VS Code), called http_impl.dart, and the error snippet goes something like this:

onError: (error) {
// When there is a timeout, there is a race in which the connectionTask
// Future won't be completed with an error before the socketFuture here
// is completed with a TimeoutException by the onTimeout callback above.
// In this case, propagate a SocketException as specified by the
// HttpClient.connectionTimeout docs.
if (error is TimeoutException) {
  assert(connectionTimeout != null);
  _connecting--;
  _socketTasks.remove(task);
  task.cancel();
  throw SocketException(
      "HTTP connection timed out after $connectionTimeout, "
      "host: $host, port: $port");
}
_socketTasks.remove(task);
_checkPending();
throw error;
});

I have tried to implement from this source and this, but when I make a request but have no connection, this error still occurs.

How to deal with this problem?
What I want is, if there is a problem with HTTP either there is no connection, or it fails to contact the server, then I can make a notification..

Is there something wrong with my code? Please help, thank you

CodePudding user response:

In Flutter, if you are experiencing HTTP connection timed out crashes, there are a few things you can try to resolve the issue:

Increase the timeout duration: You can increase the duration for which the app will wait for a connection before timing out. This can be done by setting the connectTimeout property of the http.Client object to a higher value.

Retry the connection: You can also implement a retry mechanism in your code, where the app will automatically retry the connection if it times out.

Check your internet connection: Make sure that the device running the app has a stable internet connection. If the connection is poor, the app may time out while trying to connect to the server.

Check your server's connection: Make sure your server is up and running and can handle the incoming requests.

Catch and handle the exception: You can also catch the exception and display a message to the user, letting them know that the connection has timed out and ask them to check their internet connection.

Use a package: There are some packages available that can handle this problem such as dio package, it has a built-in mechanism to handle timeouts and retries.

It's important to note that the solution that works best for your app will depend on the specific requirements of your app and the reasons why the connections are timing out.

CodePudding user response:

  1. You re throw the exception in your code, You need to catch exception where you call to this method like this.

    try { await fetchData(); } catch (e) { // TODO: handle exception }

  2. You can stop VS Code catching unhandled exceptions from this way https://superuser.com/a/1609472

  • Related