Allen Hambacher
Allen Hambacher

Reputation: 21

About a local connection between Flutter and Python

I would like to connect Flutter with a Python backend. Several tutorials such as ML models using flask or online tutorials suggest to use the REST-API of FLASK to connect Flutter and Python.

Usually, a localhost is created with FLASK and the flutter app gets the information via the http package.

I need to create a flutter-python connection, that is very secure and local, that means, that when the App is released, the Python function may not work on a server, but on the phone.

Is is possible to realize the procedure of running the Flutter frontend initializing the Flask backend on a phone?

Upvotes: 2

Views: 1035

Answers (1)

PvChat-Bot
PvChat-Bot

Reputation: 11

I did some research and found an answer to hopefully point you on the right track. The solution looks similar. Let me know!

https://stackoverflow.com/a/62308032/14514188

https://github.com/rikulo/socket.io-client-dart#usage-flutter

In Flutter env. it only works with 
dart:io websocket, 
not with  
dart:html websocket, 



so in this case you have to add 'transports': ['websocket'] when creates the socket instance.

IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
    'transports': ['websocket'],
    'extraHeaders': {'foo': 'bar'} // optional
});
The default Flask development server doesn't support websockets so you'll need to use another server. Thankfully it's simple to get eventlet working with Flask. All you should have to do is install the eventlet package using pip.

pip install eventlet Once eventlet is installed socketio will detect and use it when running the server.

You can use chrome to double check what transport method is being used. Open your chrome dev tools Ctrl+Shift+I in Windows and go to the Network tab. On each network request you should see either transport=polling or transport=websocket

https://stackoverflow.com/a/62308032/14514188

Upvotes: 1

Related Questions