API works on postman but refuses to connect in Flutter
P粉207483087
P粉207483087 2024-04-06 14:08:33
0
1
461

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粉207483087
P粉207483087

reply all(1)
P粉070918777

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template