How to point to localhost:8000 with the Dart http package in Flutter?

77,183

Solution 1

Short answer: You can pass an Uri instead of a string as parameter

      var client = createHttpClient();
      client.get(new Uri.http("locahost:8000", "/category"));

Solution 2

Replacing the string localhost with 10.0.2.2 resolved it for me, since I was running the code in the Android emulator, which is running in a VM. It's essentially a duplicate of this question.

Solution 3

replace 'localhost' in your url to wifi connection ip e.g : 'http://localhost:8000' => 'http://192.168.1.102:8000'. you can get your wifi ip from command prompt with cmd>ipconfig (wireless LAN adapter WI-FI.

var url = 'http://192.168.1.102:8000';
Future<String> getUnits(String category) async {
    var response = await httpClient.get('$url/$category');
    return response.body;
}

Solution 4

If you are using an Android emulator then localhost on the emulator is not 127.0.0.0 it is 10.0.2.2, so, on Android emulator you need to write https://10.0.2.2:8000, the https://127.0.0.1:8000 will not work on real device too. because localhost means something different on real device.

For more information on how to connect a Flutter app to localhost on emulator or on a real device click on the link Connecting Flutter application to Localhost

Solution 5

Try forwarding the port of your emulator or the device to your computers port

e.g if your server is running on localhost:8000 then run this command

adb reverse tcp:8000 tcp:8000

this command actually redirects your phone’s port 8000 to your computer’s port 8000.And now your client should be able to talk to the server running locally

Tip: this also works if you want to run your flutter web app on phone using flutter run -d webserver more details here

Share:
77,183
Mary
Author by

Mary

Updated on February 10, 2022

Comments

  • Mary
    Mary about 2 years

    I'm following the Flutter Networking/HTTP tutorial to do a GET request to a server running on my localhost:8000. Visiting my localhost via my browser works fine. My code looks like this:

    var url = 'http://localhost:8000';
    Future<String> getUnits(String category) async {
        var response = await httpClient.get('$url/$category');
        return response.body;
    }
    

    This works fine when I point to any real URL, such as https://example.com, but when I point to https://localhost:8000 or https://localhost (or any variations of these), I get an error starting with:

    E/flutter ( 4879): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
    E/flutter ( 4879): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 47060
    E/flutter ( 4879): #0      IOClient.send (package:http/src/io_client.dart:30:23)
    

    The port in the error above changes each time I reload the app. I looked in the http package code and it doesn't seem like there is a way to specify the port for the URL. How do I point to my localhost?