0
Future getSearchData() async {
    var srch = {'Search': query};
    var data =
        await http.post("http://10.0.2.2/DBCON/searchspec.php", body: srch);
    var databody = jsonDecode(data.body);
    return databody;
  }

I am trying to get data from my localhost and print it in my flutter application, the problem is it is working perfectly in Android Emulator, but it doesn't work in my physical device, can you tell me what to do in order to make it work in my physical device?

Tarek
  • 1
  • 2

1 Answers1

0

10.0.2.2 is a special alias to the localhost in your PC, which means your Emulator can access your system's locally hosted API through this special address. Emulators don't have network capabilities inherently, which is why this provision is in place.

Your physical device however, doesn't have any such means.

You'd need to connect through the Internal IPv4 address which is provided by the Network Gateway (usually a wireless router) to your System. Use ipconfig/ifconfig/ip addr to obtain it.

This requires both devices to be in the same network. Once obtained, use that address instead of 10.0.2.2.

Here's one answer which adopts this solution (without reasoning though)

Arvind
  • 614
  • 5
  • 5