Connection TimeOut in flutter when making a request to django Rest api locally

432 Views Asked by At

void reg() async { try{ http.Response response = await http.get(Uri.parse("http://10.0.2.2/api/alluser")); print(response.body); } catch(e){ print(e); } }

It is working when I am doing in chrome( web) http://127.0.0.1:8000 but not working in android emulator.

in Django's setting.py

ALLOWED_HOST = [10.0.2.2 , 127.0.0.1]

1

There are 1 best solutions below

0
Dhruv Patel On

NOTE - Specific for connecting Android and django rest api in Flutter

I tried lot of things but connection timeout was occuring. I was using django Rest-Api

  1. go to Command Prompt type ipconfig copy ipv4 address in my case 192.168.2.4. enter image description here
  1. add 192.168.2.4 in ALLOWED_HOST =[] in django settings.py.
  2. type "python manage.py runserver 192.168.2.4:8000" to run your server. 4)In Flutter your baseurl for accesing django rest api should be "http://192.168.2.4:8000/"

This worked for me . thanks