9

Flutter can binding to native code using dart:ffi. In other words, Dart can call C ++ methods, but can C ++ call Dart methods?

as follows:

截屏2020-05-08下午5.55.13.png

Liu Silong
  • 4,902
  • 3
  • 19
  • 28
  • Yes. If the callback is occurring on the main dart thread it's relatively easy. It's more complicated when a second thread it's involved. – Richard Heap May 08 '20 at 10:10
  • 2
    Does this help? https://stackoverflow.com/questions/61541354/dart-flutter-ffi-foreig-function-interface-native-callbacks-eg-sqlite3-exec – Richard Heap May 08 '20 at 10:32
  • Dart is a scripting language. It can be executed in a virtual machine, like native code, in a Java machine, and in other ways. It is hardly possible what you need. Dart does not export symbols. – mezoni May 08 '20 at 12:29
  • 4
    @mezoni your comment is incorrect. This is exactly what https://api.dart.dev/stable/2.8.1/dart-ffi/Pointer/fromFunction.html is used for. – Richard Heap May 12 '20 at 02:24
  • Maybe your diagram is not quite correct. If I understand you correctly, then you want to call C++ code from Java and call Dart code from C++ code. This is hardly possible. But this is possible if you call C++ code from Dart code by passing it a pointer. What does Java code have to do with calling Dart code from C++ code? Java -> C++ -> Dart? – mezoni May 12 '20 at 06:15
  • @mezoni Your understanding of the diagram is correct. I want to try to write an Android plug-in for Flutter. This plug-in allows Java to communicate with Dart through FFI. The current situation is that with the help of FFI, Dart can tell Java to do something, but the reverse is not true. I want Java to notify Dart. – Liu Silong May 12 '20 at 10:39
  • Have you tried checking [this blog](https://levelup.gitconnected.com/port-an-existing-c-c-app-to-flutter-with-dart-ffi-8dc401a69fd7)? It seems that the blog discussed about calling native C/C++ code in Flutter. – MαπμQμαπkγVπ.0 Jun 01 '22 at 18:08
  • @MαπμQμαπkγVπ.0 Thanks!What I mean is that Java has JNI that allows C++ to call Java methods, Dart's FFI is to let Dart call C++ methods, and it seems that there is no ”DNI“ that allows C++ to call Dart methods – Liu Silong Jun 06 '22 at 04:09
  • @mezoni dart is not a scripted language that runs in a VM, it compiles to a native binary – Dominic Aug 23 '23 at 13:56

1 Answers1

0

From the comments it seems that you want to create an Android specific plugin for Flutter.

This does not require C++ in itself, you usually use Kotlin or Java code a the system level.

The Developing packages & plugins guide may be useful here.

Ber
  • 40,356
  • 16
  • 72
  • 88