Encountering a timeout error when linking Flutter with my API

Connecting Flutter to an API yields a timeout error. Below is an alternative code snippet:

Future<void> loadDataRecord() async {
  final String endpoint = 'http://192.168.1.100:5000/data';
  try {
    final response = await http.get(Uri.parse(endpoint));
    if (response.statusCode == 200) {
      print(json.decode(response.body));
    } else {
      throw Exception('Request unsuccessful');
    }
  } catch (error) {
    print(error);
  }
}

Any suggestions to resolve this issue?

Based on my experience working with Flutter and local API connections, I believe the issue could stem from either network connectivity or server-side configurations. I faced a similar problem when the local server was not properly accessible from the device, and I had to adjust firewall settings and verify the network interface binding. Verifying that the device is on the same network as the server helped in my case. Additionally, checking if there’s a mismatch in protocol expectations or settings in the http client can sometimes resolve the timeout issues.

In my experience, after verifying that network configurations and device connections are correct, I discovered that my server was restricted to localhost connections. Changing this setting allowed the server to accept incoming requests from external devices. It might be helpful to check your API server’s listening address or test the setup with a tunneling tool like ngrok to see if external access is the problem. Additionally, double-check that the API port is open and not blocked by any firewall rules, as this could also be a cause of the timeout.

hey, i ran into this issue before when my api was only listening on localhost. make sure it’s bound to 0.0.0.0 and that your device is on the right network. sometimes a firewall can block reqs, so check that too.