5

I'm trying to understand how to test http calls from my flutter app (which is running in an emulator) to my backend service, which is running in debug on my pc on https://localhost:8080.

I get that talking to https://localhost:8080 directly from flutter won't work because it means "emulator's localhost", so I'm talking to my pc local ip, but I got CERTIFICATE_VERIFY_FAILED because of course https://192.168.1.123:8080 doesn't have a signed cert.

What is the correct way to handle this situation?

Doc
  • 5,078
  • 6
  • 52
  • 88

1 Answers1

8

You need to use the localhost of your host machine, not the localhost of your emulator. To do that you need to use 10.0.2.2 and then your port number, so in your case make http calls to https://10.0.2.2:8080. Internally Android reroutes calls to 10.0.2.2 to 127.0.0.1, which is the localhost ip on your dev machine.

How do you connect localhost in the Android emulator?

Some more info: https://developer.android.com/studio/run/emulator-networking.html

JJuice
  • 1,015
  • 2
  • 12
  • 21
  • thanks. should I have to fallback to http or can i stick with https? – Doc Mar 31 '20 at 11:12
  • I'm not sure about that, I personally use http. I don't think https works out of the box, I found an article on Medium on setting up SSL for dev purposes. https://medium.com/@noumaan/ssl-app-dev-a2923d5113c6 – JJuice Mar 31 '20 at 11:21
  • does this apply to iOS emulator/device as well? Thanks. – Kishor Tiwari May 24 '21 at 11:30
  • 1
    If you mean by ‘this’ the 10.0.2.2 IP address: no, this is for Android only. For iOS you can just use the normal 127.0.0.1 – JJuice May 24 '21 at 13:08