Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include C file in a dart/flutter project

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!

like image 313
CryptUser Avatar asked Aug 13 '18 10:08

CryptUser


People also ask

Can I use C in Flutter?

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.

How do I import files into Flutter?

Use the command ctrl+shift+p to open Command Palette. Type Flutter: New Project in the palette.

Can I code in C++ for Flutter?

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.

How do I show files in Flutter?

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.


1 Answers

Currently, there is no way you can do this synchronously. Sorry for breaking your heart.

like image 172
Vns Aditya Avatar answered Sep 28 '22 10:09

Vns Aditya