https://github.com/ggdream/call
Flutter: Calling dynamic link library defined in the assets field of pubsepc.yaml file. (Support Windows, Linux and MacOS)
https://github.com/ggdream/call
call cross-platform dart dylib ffi flutter linux macos native windows
Last synced: 9 months ago
JSON representation
Flutter: Calling dynamic link library defined in the assets field of pubsepc.yaml file. (Support Windows, Linux and MacOS)
- Host: GitHub
- URL: https://github.com/ggdream/call
- Owner: ggdream
- License: mit
- Created: 2021-03-06T04:10:32.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-19T04:32:23.000Z (over 5 years ago)
- Last Synced: 2023-08-20T21:30:41.056Z (almost 3 years ago)
- Topics: call, cross-platform, dart, dylib, ffi, flutter, linux, macos, native, windows
- Language: Dart
- Homepage: https://pub.dev/packages/call
- Size: 59.6 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# call
You can use the package to open the dylib defined in the `assets` field of `pubsepc.yaml`.
## 1.Prepare your dynamic link library
- Write your C-code
```c
// file: ${PROJECT_ROOT}/assets/main.c
int add(int a, int b) {
return a + b;
}
```
- Compile it to a dylib
```sh
gcc -shared -fPIC -o libadd.so main.c # Linux
gcc -shared -fPIC -o libadd.dll main.c # Windows
clang -shared -fPIC -o libadd.dylib main.c # MacOS
# 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.
~~~yaml
flutter:
assets:
- assets/libadd.dll # Fill it in according to your storage location
~~~
## 3. Write flutter core code to call native function.
~~~dart
import 'package:flutter/material.dart';
import 'dart:ffi' as ffi;
import 'package:call/call.dart';
typedef FuncNative = ffi.Int32 Function(ffi.Int32, ffi.Int32);
typedef FuncDart = int Function(int, int);
void main() => runApp(App());
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State {
@override
Widget build(BuildContext context) {
var dll = getDyLibModule('assets/libadd.dll'); // use it as images.
var add = dll.lookupFunction('add');
return Text(
add(999, 54639).toString(),
textDirection: TextDirection.ltr
);
}
}
~~~
## 4. Run the flutter application
Finally, You can see the number `55638` in the top left corner of the application.