Flutter http: No route to host when trying to send a request to localhost:5000

165 Views Asked by At

I am trying to use a Flutter app running on a device (and debugging via Android Studio) to make a simple GET request to my localhost:5000. The response should be the result of a stored procedure being run on SQL Server, shown below:

{"recordsets":[[{"SomeTimestamp":"2023-06-22T13:43:36.120Z","OtherField":""}],[{"":["1-1-2023","2nd field of 2nd line"]}]],"recordset":[{"SomeTimestamp":"2023-06-22T13:43:36.120Z","OtherField":""}],"output":{"Status":null},"rowsAffected":[1,1],"returnValue":0}

When I try to use the http Flutter package to send a request, Android Studio shows an error in the Run tab that says:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: No route to host

The code I tried, inside of my login.dart:

  @override
  void initState() {
    super.initState(); //Run this first
    initialize();
  }


  void initialize() async {
    var url = Uri.https('10.0.2.2:5000');
    var response = await http.get(url); //Error points here
    if (kDebugMode) {
      print('Response status: ${response.statusCode}');
    }
    if (kDebugMode) {
      print('Response body: ${response.body}');
    }

    if (kDebugMode) {
      print(await http.read(url));
    }
  }

Update: when I use the Android Studio emulator (instead of the physical device), it works just fine. So it seems as though the physical device is having trouble finding 10.0.2.2:5000, even though it is connected to the building's wifi. Note - the machine I'm using to act as the local server is connected via ethernet, but it's all the same network.

0

There are 0 best solutions below