I made an API from which I can get data using http.request module in dart. 'http://10.0.2.2:5000' is the url I'm using right now and it works just fine on my emulator but not on my phone, any idea why? Also does it make a difference if the phone is connected to the pc via USB? Thanks in advance
Asked
Active
Viewed 464 times
1 Answers
0
10.0.2.2
is the loopback address used inside the network address space of Android Emulator, so only works wether you are using the emulator.
When you are using a physical device, the loopback address is 127.0.0.1
.
Try with 'http://127.0.0.1:5000' .
Of course, be sure to have INTERNET
permissions.

Maffe
- 430
- 6
- 14
-
Tried using the address you gave me but it throws the error: Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111 (Also I added INTERNET and ACCESS_NETWORK_STATE permissions) – geomikeo Jan 14 '20 at 19:29
-
are both server and client on the same phone? Or the server is on another device? – Maffe Jan 15 '20 at 09:10
-
The server is on my pc – geomikeo Jan 15 '20 at 15:29
-
the loopback address only works wether both client and server are on the same device. In your case just follow [this answer](https://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device). – Maffe Jan 15 '20 at 16:18