Python in Flutter App

Hilal Ürün
Flutter Students Club
2 min readFeb 6, 2022

Here is the story ..

Assuming that you are familiar with Flutter 💙 and Python 🐍. In this article, I’m going to share my experience about how can run python scripts in our flutter application which is our main focus. There are several ways to do this such as Starflut and via API call we’re going to do together✨.

When we think of things that we can do in Python but cannot do in other languages, we can say easily Machine Learning (ML). Then we have a scenario which is we have a flutter application uses the some ML model from a python script with an API call. For this purpose we need to use http package in flutter app.

According to pub.dev instructions we are install the http package using as a library with below command and after run this command we will see its added your package’s pubspec.yaml.

Now we create a new file named flask.dart in our Flutter application. For using http package we also need to import package in our Dart code and add following code block in our file:

That’s all for flutter side for now. It’s Python turn, we can open our Python IDE and then we can create a Flask project. If you have no choose like that, just create a python project and add code which is attached below on your python script.

When we will run our projects it runs on local host port 5000 (http://127.0.0.1:5000/). hello_world function is returning a string but for getting output from Flutter we need to change this part and it should returns a json file.

Turning back to Flutter framework! Furthermore, we are going to our main.dart script and insert below peace of code where we want to access and get the data.

That’s all now we have our data 💙 You can also use other methods but in my opinion the easiest way to do it is using API 🚀. This was very entry level experience, we will talk about more complex operations with Python in Flutter.

Soon..

--

--