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

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.


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;
}

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

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