Home  >  Q&A  >  body text

API works on postman but refuses to connect in Flutter

postRequest() async {
    Map\<String, dynamic\> map = {
        'email': '[email protected]',
        'password': '1234',
    };

    try {
        var url = Uri.parse('http://127.0.0.1:8000/api/login');
        var response = await http.post(url,
            headers: {
                'Accept': 'application/json'
            },
            body: jsonEncode(map)
        );

        if (response.statusCode == 200) {
            var responseBody = jsonDecode(response.body);
            print(responseBody);
            return responseBody;
        } else {
            print("try error ${response.statusCode}");
        }
    } catch (e) {
        print("Error catch ${e}");
    }
}

mistake:

I/flutter (25242): Error catching connection refused

P粉207483087P粉207483087219 days ago444

reply all(1)I'll reply

  • P粉070918777

    P粉0709187772024-04-07 00:57:07

    Try hosting your app on a local IP and use the same wifi or network your phone is connected to. like:

    If you are using Windows:

    For Wi-Fi connection

    1. Select Start > Settings > Network & Internet > Wi-Fi, and then select The Wi-Fi network you are connected to.

    2. Under Properties, look for your IP address listed next to IPv4 address.

    For Ethernet connection

    1. Select Start > Settings > Network & Internet > Ethernet.

    2. Under Properties, look for your IP address listed next to IPv4 address.

    If you use Ubuntu:

    Looks like 192.168.x.x For example:- 192.168.0.100, 192.168.1.20..etc

    Open the terminal of the root folder you want to host. Then run this command

    You need to change this IP to your IP 192.168.x.x and port because you will be hosting "I prefer 8000" and use that IP address and port in your Flutter app.

    reply
    0
  • Cancelreply