Reputation: 353
I am trying to load in a dynamic library and then run a long running native function in another isolate.
I can get this to work if I make everything global but this wont work for me as I need to do it in an initialisation function which is called after certain required things are initialised.
What makes isolates different from threads is making this "simple" task very difficult.
What I want:
typedef _VoidVoidFunction = void Function();
typedef _VoidVoidFunctionNativeFunction = Void Function();
class NativeInterface {
late final Future<DynamicLibrary> _dLib = _initialise();
@override
Future<void> initialise() async {
await _dLib;
}
Future<DynamicLibrary> _initialise() async {
final String libPath = "...";
final DynamicLibrary dLib = DynamicLibrary.open(libPath);
final _VoidVoidFunction nativeStartBackend = dLib.lookupFunction<
_VoidVoidFunctionNativeFunction,
_VoidVoidFunction
>("start_backend");
// This just errors and crashes the app
Isolate.run(nativeStartBackend);
return dLib;
}
}
I have tried to use Isolate.spawn
but because it has to use a top level function, that makes it very hard to have access to the dynamic library and the native function that is created in the initialise function.
Is there anyway I can get nativeStartBackend
to run in an isolate? At this point I think it might be better to use a thread in the dynamic library and write an extra wrapper in there to make that work, but I'd rather make it work here.
Edit: I should have said that calling the native function not in a new isolate, does work, just to prove that its not what is causing the crash, it just hangs the UI as expected, as it doesn’t return.
Upvotes: 0
Views: 85