我被困在如何在 Flutter 中实现dart::ffi ,特别是hello_world 示例。
从基本flutter create
项目(和已经编译的hello_world.dll
)开始,只是尝试:
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());
}
导致:
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):
我是否还需要一个.dylib
版本,例如示例中的内容?
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');