21

I am developing an application in Flutter (for Android) whose core is based on a library developed only in C++ and python. Therefore, I would like to know how I can use this library in my dart code (either the C++ version or the Python version).

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
Endy Bermúdez R
  • 351
  • 1
  • 2
  • 8
  • 3
    can you give some sample code (both Dart and C++), otherwise @Xavier answer seem to be the one. – TruongSinh Mar 02 '19 at 10:43
  • 4
    Try this [video](https://www.youtube.com/watch?v=X8JD8hHkBMc) by Richard Heap, it helped me quiet a lot. He explains with an example how to interact between the Dart code of your Flutter app, and the C++ library you're using, all through ```dart:ffi```. I'd also recommend checking out this [post](https://stackoverflow.com/questions/62351258/debug-c-code-from-dart-package-using-dartffi) for a way to debug the C++ code while running it through a Flutter app. – Facundo Farall Jun 18 '20 at 18:29

1 Answers1

13

Additional information (2021-05-05)

From @FacundoFarall comment to the original question:

Try this video by Richard Heap, it helped me quiet a lot. He explains with an example how to interact between the Dart code of your Flutter app, and the C++ library you're using, all through dart:ffi. I'd also recommend checking out this post for a way to debug the C++ code while running it through a Flutter app.

Update (2019-09-10)

Today has been released Dart 2.5 (and Flutter 1.9). One of the additions to Dart 2.5 is "the dart:ffi foreign function interface for calling C code directly from Dart." I still haven't checked it, but it should provide a more direct path to interface with C (and C++) code.

See also the announcement video (confirms both old and new methods).

Old answer (still valid)

I would take a look at Platform Channels, which are a way to communicate Flutter code to platform native code (e.g. Java/Kotlin on Android, Objective C/Swift in iOS). From there, you can use your C++ library using the regular NDK mechanisms.

You can see an introduction to Flutter Platform Channels and Getting Started with the NDK.

Maybe there's a more direct route, but that's the one I know could work.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50