Implementing Python-API into Flutter/Dart framework

Valentin Ahrend
3 min readMay 28, 2022

For me, it was not possible to start python code inside the flutter application, that’s why I had to options:

  • Option 1: Deploy the Python Flask Server to a real web server (e.g. Heroku)
  • Option 2: Translate parts of the Python-API into Dart and only deploy the dependency calls to a web server.

Pro & Cons for Option 1

All python code in one big container. The process itself is perfectly connected to its single parts. It is much easier to just deploy the server to a real web server.

It is not possible on a free level. Heroku, a free server hosting service, is not capable of such a big program (>500mb). The memory (RAM) of the a free Heroku server is also not big enough to load two dependencies (e.g. germanet, spacy) at the same time.

Pro & Cons for Option 2

The python code is split into fragments, there is no good overview and binding. It is harder and takes some time to translate and split the program.

It is free and will work at the same performance level as the python code. This is because Python is a scripting language and is ‘slow’ compared to Dart. This speed difference will balance out the delay between web servers and the Dart code.

I choose Option 2, the final and main reason was that I am capable to translate and handle the program, because I wrote is myself and know every dependency, as well as that even though I was motivated to pay for server expenses, no web server provider offered a simple and ‘cheap’ tooling, because of the high requirements this project has.

Translating dependencies

I started translating GermaNet, which is not complex at all in its algorithms. GermaNet is a lexical unit system (like WordNet) that provides a word tree and can compute the semantic distance between two words using 6 different methods. In fact: The main code of GermaNet is just the resource loader of the GermaNet files (which are licensed). The translation of Python-GermaNet resulted in the dart package germanet_dart.

Deploying single dependency to Heroku web server

There are some dependencies that are very complex in their algorithms but also their language diversity. The complexity makes the dependencies hard or even impossible to translate, so that the best option is to deploy the single fragment to a web server that can be connected to Dart code. I did this with spacy: a model based natural language processing tool.

Rebuilding the API inside a Dart package

Because all dependencies are online or translated to Dart, I created a private dart package.

$ dart create --template=package-simple dart-project-api

Then I started with translating my python source code into the Dart package and finally converted the Python-API (a Flask Server) into a Dart script that can be run inside a Flutter application. For using the package I added the package into the pubspec.yaml file via:

dependencies:
library_name:
path: /path/to/library_name

--

--

Valentin Ahrend

Self taught software developer. Sharing some ideas, experiences and projects.