7

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

Zahra
  • 2,231
  • 3
  • 21
  • 41
Taha Ali
  • 71
  • 1
  • 1
  • 3
  • 1
    We need some code to be able to help you. – Martin Niederl Jun 25 '19 at 16:06
  • @MartinNiederl Should i provide the whole script ? – Taha Ali Jun 28 '19 at 11:03
  • Depending on whether your device is running in an emulator or on an external device, the IP address for accessing the local server must be adjusted. Here's a little explanation: https://stackoverflow.com/a/34732276/6513167 I don't know your setup, so I also don't know how to reach your local server. – Martin Niederl Jun 29 '19 at 10:54

5 Answers5

12

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

Zahra
  • 2,231
  • 3
  • 21
  • 41
  • Discovering this forced me to create the (long overdue) network and internet connectivity features for my app. Hopefully it won't catch me again! – dKen Jun 20 '22 at 07:50
4

Turning OFF the Wi-fi on emulator helped me!

Dmitrii Matunin
  • 275
  • 4
  • 5
3

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.

Hawkar Shwany
  • 324
  • 3
  • 15
1

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);
      }
wafL
  • 577
  • 4
  • 14
0

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 :~

Akram Esg
  • 23
  • 5