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

前端 未结 9 1063
野趣味
野趣味 2020-12-10 00:34

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 lo

相关标签:
9条回答
  • 2020-12-10 01:08

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

          var client = createHttpClient();
          client.get(new Uri.http("locahost:8000", "/category"));
    
    0 讨论(0)
  • 2020-12-10 01:10

    im using ubuntu 20LTS, laravel backend, flutter http package. first i installed a package by sudo apt install net-tools. this package supports ifconfig command to work. then from the output of ifconfig command, lookout for inet 192.168.43.217 netmask 255.255.255.0 broadcast 192.168.43.255 this line. copy inet 192.168.43.217 this ip address. from laravel project run sudo php -S 192.168.43.217:81 -t public to serve to inet address . then from flutter _apiRoute = "http://192.168.43.217:81/api/login". this worked for me.

    0 讨论(0)
  • 2020-12-10 01:12

    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

    0 讨论(0)
  • 2020-12-10 01:14

    If you are trying to access localhost api through emulator, Change localhost to your IPV4 Address. And if you are running your api in Visual Studio set the app URL also to be IPV4 address. In my case I changed it from "localhost:5001" to 192.168.XX.XX:5001

    if you don't change the backend it will return “Bad Request-Invalid Hostname” When accessing localhost from emulators

    0 讨论(0)
  • 2020-12-10 01:15

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 01:15

    Localhost via the computer browser I assume?

    Using "http://localhost:port" in the Flutter code points to the local emulator device and not the local server running on your coomputer.

    Point url to the IP Address of your server machine instead, that should solve the issue.

    0 讨论(0)
提交回复
热议问题