user14713384
user14713384

Reputation:

Unable to connect flutter with flask backend on local host

I am trying to connect flutter to my flask backend. But flutter gives following error

Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 50512

I even tried using my ipv4 address even localhost even host 0.0.0.0 and tried various ports but everytime the port keeps changing in flutter error. Sometimes like this

 Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 39223

Can anyone suggest how do I solve this. Here is my flask code

port = int(os.environ.get('PORT', 5000))
if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=port) // changing to 0.0.0.0 also doesn't work neither different ports

I am able to connect to heroku server but not on localhost.

Upvotes: 0

Views: 1477

Answers (2)

Mary
Mary

Reputation: 517

Get the local IPv4 address of your computer then run the flask app with the acquired IPv4 address (flask run -h 192.168.X.X )

in flutter app use this Ip

http.get('192.168.X.X:5000/api/user')

Upvotes: 0

user13116294
user13116294

Reputation:

Try giving 'localhost' as host in app.run() and port 5000. It will work on your real device. If using emulator then can use "10.0.2.2". Let me know if it works for you.

Upvotes: 3

Related Questions