0

I'm stuck on how I would implement dart::ffi in Flutter, specifically the hello_world example.

Starting with the basic flutter create project (and an already compiled hello_world.dll) just trying:

void main() {
  final dylib = ffi.DynamicLibrary.open('hello_world.dll');
  final HelloWorld hello = dylib
      .lookup<ffi.NativeFunction<hello_world_func>>('hello_world')
      .asFunction();
  hello();
  runApp(MyApp());
}

leads to:

Launching lib\main.dart on sdk gphone x86 arm in debug mode...
package:testing_flutter/main.dart:1
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:61913/wRTGFN76kik=/ws
E/flutter (11980): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "hello_world.dll" not found)
E/flutter (11980): #0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:55)
E/flutter (11980): #1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:20:12)
E/flutter (11980): #2      main
package:testing_flutter/main.dart:8
E/flutter (11980): #3      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:140:25)
E/flutter (11980): #4      _rootRun (dart:async/zone.dart:1354:13)
E/flutter (11980): #5      _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (11980): #6      _runZoned (dart:async/zone.dart:1788:10)
E/flutter (11980): #7      runZonedGuarded (dart:async/zone.dart:1776:12)
E/flutter (11980): #8      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:133:5)
E/flutter (11980): #9      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (11980): #10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter (11980):

Would I also need a .dylib version, something like what's in the example?

  var libraryPath =
      path.join(Directory.current.path, 'hello_library', 'libhello.so');
  if (Platform.isMacOS)
    libraryPath =
        path.join(Directory.current.path, 'hello_library', 'libhello.dylib');
  if (Platform.isWindows)
    libraryPath = path.join(
        Directory.current.path, 'hello_library', 'Debug', 'hello.dll');
Jonathan Woollett-light
  • 2,813
  • 5
  • 30
  • 58
  • ffi is a bit different for Flutter since you're usually in a mobile environment. See [this](https://flutter.dev/docs/development/platform-integration/c-interop) for some useful reference on ffi in Flutter. – Christopher Moore Mar 19 '21 at 22:08
  • Hi, due to the differences of iOS and Android builds you find that there's no shared library on iOS, but there is one for Android. It's simplest to just have the Flutter build system build your C/C++ code as part of its normal build. You really need to start with a plugin project (which conveniently includes a Flutter example project for testing). I gave a talk at a meetup a year ago. Scroll down to a link to the slides - sorry there's no video. Feel free to comment with any questions: https://www.meetup.com/flutter-nyc/events/263788680/ – Richard Heap Mar 30 '21 at 00:56

0 Answers0