如何在 Flutter 中使用 dart:ffi?

问题描述

我一直在思考如何在 Flutter 中实现 dart::ffi,特别是 hello_world example

从基本的 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 版本,例如 in the example

  var libraryPath =
      path.join(Directory.current.path,'hello_library','libhello.so');
  if (Platform.isMacOS)
    libraryPath =
        path.join(Directory.current.path,'libhello.dylib');
  if (Platform.isWindows)
    libraryPath = path.join(
        Directory.current.path,'Debug','hello.dll');

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...