In a cross-platform (Android/iOS) project written with Dart and Flutter, I need to include a C-file and call C-functions from that file. For example, lots of the latest encryption code does not exist in Dart yet. Furthermore, I need to call the C code in dart functions with return values, so I cannot use asynchronous methods, but need to call these functions synchronously.
A thread about including C/C++ code in Flutter seems to suggest to use platform channels. With these, one could use native code of the respective platforms. For iOS, Objective C is a superset of C, and for Android, one would need an additional layer, namely the NDK to use C code. This would not only mean that one needs to write the code twice (which I could live with), but the platform channels tutorial explicitly says that the platform calls are asynchronous (which breaks things for me).
I found another thread about native extensions in Dart from 2012 which shows how to include C code in dart, but this appears to be aimed only at command line apps.
It would be strange if there exists no way to synchronously call a C function from flutter. If you have an idea of how to make this happen, you could for a proof of concept consider a trivial C function like this:
int inc(int num) {
return num + 1;
}
Thanks in advance!
Dart mobile, command-line, and server apps running on the Dart Native platform can use the dart:ffi library to call native C APIs, and to read, write, allocate, and deallocate native memory. FFI stands for foreign function interface.
Use the command ctrl+shift+p to open Command Palette. Type Flutter: New Project in the palette.
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.
To list all the files or folders, you have to use flutter_file_manager, path, and path_provider_ex flutter package. Add the following lines in your pubspec. yaml file to add this package in your dependency. Add read / write permissions in your android/app/src/main/AndroidManifest.
Currently, there is no way you can do this synchronously. Sorry for breaking your heart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With