项目作者: ggdream

项目描述 :
Flutter: Calling dynamic link library defined in the assets field of pubsepc.yaml file. (Support Windows, Linux and MacOS)
高级语言: Dart
项目地址: git://github.com/ggdream/call.git
创建时间: 2021-03-06T04:10:32Z
项目社区:https://github.com/ggdream/call

开源协议:MIT License

下载


call



😭
😭

You can use the package to open the dylib defined in the assets field of pubsepc.yaml.





  • Write your C-code
    ```c
    // file: ${PROJECT_ROOT}/assets/main.c

int add(int a, int b) {
return a + b;
}

  1. - Compile it to a dylib
  2. ```sh
  3. gcc -shared -fPIC -o libadd.so main.c # Linux
  4. gcc -shared -fPIC -o libadd.dll main.c # Windows
  5. clang -shared -fPIC -o libadd.dylib main.c # MacOS
  6. # file: ${PROJECT_ROOT}/assets/libadd.dll

2. Declare the assets path

You should declare path of the dylib in the pubspec.yaml file as images.

  1. flutter:
  2. assets:
  3. - assets/libadd.dll # Fill it in according to your storage location

3. Write flutter core code to call native function.

  1. import 'package:flutter/material.dart';
  2. import 'dart:ffi' as ffi;
  3. import 'package:call/call.dart';
  4. typedef FuncNative = ffi.Int32 Function(ffi.Int32, ffi.Int32);
  5. typedef FuncDart = int Function(int, int);
  6. void main() => runApp(App());
  7. class App extends StatefulWidget {
  8. @override
  9. _AppState createState() => _AppState();
  10. }
  11. class _AppState extends State<App> {
  12. @override
  13. Widget build(BuildContext context) {
  14. var dll = getDyLibModule('assets/libadd.dll'); // use it as images.
  15. var add = dll.lookupFunction<FuncNative, FuncDart>('add');
  16. return Text(
  17. add(999, 54639).toString(),
  18. textDirection: TextDirection.ltr
  19. );
  20. }
  21. }

4. Run the flutter application

Finally, You can see the number 55638 in the top left corner of the application.