4

I am making a flutter application where I have my backend as C++. Through dart ffi I am signalling a load call when user enters a certain screen. My problem is Im loading audio in C++ but I want to signal dart that the loading has finished so that I can stop showing a loading screen. AFAIK, there is no way to call dart from C++. I have seen that you can use FromFunction. Source here.

How can I call a void class method with no parameters? or How can I call dart code from C++?

Ps: I think this is impossible to do this as of now (without work arounds). Dart devs please add this feature as it is very useful to be able to make calls to dart to process information

cs guy
  • 926
  • 2
  • 13
  • 33
  • perhaps this https://flutter.dev/docs/development/platform-integration/platform-channels helps – Bernd Nov 20 '20 at 22:41
  • this is for dart to java and vise versa, I need c++ to dart sadly – cs guy Nov 20 '20 at 22:45
  • Perhaps there is no direct way - but maybe you could use other channels - like network / sockets... – Bernd Nov 20 '20 at 22:49
  • How can i use sockets to do this? any resources? – cs guy Nov 20 '20 at 22:49
  • I know just C++ not Dart. But the general idea is to open a socket and listen for messages. With such messages you can do what ever you like. – Bernd Nov 20 '20 at 22:56
  • 1
    see here: https://stackoverflow.com/questions/51077233/using-socket-in-flutter-apps – Bernd Nov 20 '20 at 22:58
  • 1
    Can you give an example code? Are you trying to call it inside a DLL/so? – SdSaati Nov 21 '20 at 04:16
  • [This answer](https://stackoverflow.com/questions/18046853/is-it-possible-to-call-dart-method-from-c/18047347#18047347) provides two options, the most direct one seems unsupported by Dart. The other involves turning your C++ code into a native function in Dart. – Ryan Deschamps Nov 24 '20 at 15:13
  • You can call Dart from C++, but only if you are on the main Dart thread (i.e. Dart calls to C which calls back to Dart in the same invocation) - see https://stackoverflow.com/questions/61541354/dart-flutter-ffi-foreig-function-interface-native-callbacks-eg-sqlite3-exec However, if you want to call Dart from a different thread - e.g. a C++ worker thread - you have to signal to Dart so that it can call you. The process for this is complicated, so there are better ways - see https://stackoverflow.com/questions/63311092/flutter-dart-how-to-use-async-callback-with-dart-ffi – Richard Heap Nov 28 '20 at 00:59
  • @RichardHeap I have seen your example and tried it. I tired to pass a class method as callback with FromFunction The problem was when I tried to compile my flutter application, I got an error saying the function had to be static in order to work. Since I can't make the function static which forces me to make every variable static inside my class – cs guy Nov 28 '20 at 13:15

1 Answers1

5

Currently, the only way to make asynchronous callbacks is through the native ports in dart_api_dl.h (and dart_api.h).

Sample code:

For more info see the issue for adding support for async callbacks to dart:ffi.

Daco Harkes
  • 296
  • 3
  • 13