A Flutter app which imports a package, wrapping a python module.
This example is intended to show how to import a Flutter package which internally uses the Dart Python FFi.
$ flutter runThis will launch the standard Flutter demo app. This app will display a counter and a button to
increment the counter. Only difference is that to increment the counter, we use
the flutter_package_export package. This package uses the
Dart Python FFi to wrap a Python module, which is then used to increment the counter.
However, we do not need to know that the counter is incremented using a Python module. We only need
import the flutter_package_export package and use it as any
other Dart package.
- None other than the standard Flutter prerequisites.
# pubspec.yaml
dependencies:
flutter:
sdk: flutter
flutter_package_export:
path: ../flutter_package_exportThe following command should be run from the root of your Flutter project. It will install the
flutter_package_export package.
$ flutter pub getWe then modify the lib/main.dart file to import the flutter_package_export package and use it
to increment the counter.
// lib/main.dart
import "package:flutter/foundation.dart";
import "package:flutter/material.dart";
import 'package:flutter_package_export/flutter_package_export.dart';
Future<void> main() async {
await initialize();
runApp(const MyApp());
}
// ...
class _MyHomePageState extends State<MyHomePage> {
// ...
void _incrementCounter() {
final int newCounter = add(_counter, 1).toInt();
setState(() {
_counter = newCounter;
});
}
// ...
}Note the only three modifications:
- Importing the
flutter_package_exportpackage. - Calling the
initializefunction in outmainfunction. - Using the
addfunction to increment the counter in the_incrementCounterfunction.
Converting all supported types between Dart and Python. See
the python_ffi_dart example.
Importing multiple Python modules in a Flutter app. See
the python_ffi example.