Dart ffi - Call Dart From C++
Asked Answered
T

1

5

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 πŸ™

Trussell answered 15/11, 2020 at 13:38 Comment(10)
perhaps this flutter.dev/docs/development/platform-integration/… helps – Bootle
this is for dart to java and vise versa, I need c++ to dart sadly – Trussell
Perhaps there is no direct way - but maybe you could use other channels - like network / sockets... – Bootle
How can i use sockets to do this? any resources? – Trussell
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. – Bootle
see here: #51077733 – Bootle
Can you give an example code? Are you trying to call it inside a DLL/so? – Dumyat
This answer provides two options, the most direct one seems unsupported by Dart. The other involves turning your C++ code into a native function in Dart. – Cotillion
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 #61541854 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 #63311592 – Mollescent
@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 – Trussell
E
7

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.

Enlace answered 17/11, 2021 at 9:8 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.