SocketException:Connection failed (OS Error: Network is unreachable, errno = 101), address = 10.0.2.2, port = 80

Viewed 12272

Making a login screen in flutter when i tap the login it gives the error 'network is Unreachable'.

I have change the ip addresses "10.0.2.2" , "8.7.7.7" but doesn't work.

Error :

E/flutter (16082): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled 
Exception: SocketException: Connection failed 
(OS Error: Network is unreachable, errno = 101), address = 10.0.2.2, port = 80

CODE :

TextEditingController user=new TextEditingController();
TextEditingController pass=new TextEditingController();

Future<List> _login() async{
  final response = await http.post("http://127.0.0.1/my_store/login.php", body: {
    "username": user.text,
    "password": pass.text,
   
  });

  print(response.body);
  }

4 Answers

In my case turning on the wifi and making sure that it is connected solved the issue.enter image description here

if you are using a physical device make sure the ip address is the ip of your computer. you can find it by running ipconfig in cmd. Remember you have to be connected to the internet to have this ip address.

I have searched some threads about this and similar connection problems. In my case, sometimes the connection works, sometimes it refuses to work. The process I used to solve this problem was the following:

Open cmd -> ipconfig

the ip that is relevant for my solution

Since I am using an Apache server, and I have a php file that handles the request I make in Flutter, I set the url to the following:

 String url="http://192.168.0.137/login.php"

In your case, the code would be

    TextEditingController user=new TextEditingController();
    TextEditingController pass=new TextEditingController();

    Future<List> _login() async{
      final response = await http.post("http://<your_ipv4_of_ipconfig>/my_store/login.php", body: {
        "username": user.text,
        "password": pass.text,

      });

      print(response.body);
      }

After many hours of head scratching, in my case it was the data connection icon was turned off by mistake cutting all sorts of data streams to vm :~

Related