Use a compiled dart executable as DynamicLibrary in Flutter

Issue

Since a longer time now it is possible to open a DynamicLibrary (dylib, dll, so) in Flutter. Those libraries are written in C or C++.

I’ve now tried to build a basic dart command line application, compiled it using dart compile exe and tried to load it in my Flutter application using DynamicLibrary.open(), as you would do with the native libraries in C/C++.

typedef HelloWorldFunc = Void Function();
typedef HelloWorld = void Function();

...

final dynLib = DynamicLibrary.open('/path/to/cli.exe');
final HelloWorld func = dynLib.lookup<NativeFunction<HelloWorldFunc>>('hello_world').asFunction();
func();

(I’ve followed this tutorial and just added an empty void function called hello_world
https://dart.dev/tutorials/server/get-started#3-create-a-small-app)

But the symbol could not be found:

Failed to lookup symbol 'hello_world': dlsym(0x7fec2310e5a0, hello_world): symbol not found

Question

Is it generally possible to open dart-compiled libraries in Flutter, like DLLs written in C++? Since dart compile exe generates native machine code as well

If yes, how?

Thanks!

Solution

Dart cannot create shared libraries like other languages can do because it needs to be run in an embedder/DartVM.

This issue has a good explanation:

https://github.com/dart-lang/sdk/issues/37480

Answered By – BEAGLE ENTERTAIN

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *