Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other than main() method this getKey() is giving error #903

Closed
harishkthedeveloper opened this issue Mar 3, 2022 · 5 comments
Closed

Other than main() method this getKey() is giving error #903

harishkthedeveloper opened this issue Mar 3, 2022 · 5 comments

Comments

@harishkthedeveloper
Copy link

harishkthedeveloper commented Mar 3, 2022

If i call this method is some other widgets this getKey method gives path error

ERROR: Failed to load dynamic library 'lib/c_lib/lib.a': dlopen failed: library "lib/c_lib/lib.a" not found

Is there any solution for this ? i want to get this working in all widgets

@harishkthedeveloper
Copy link
Author

Below is the code i am using from link - https://stackoverflow.com/questions/63747683/how-to-call-go-lib-from-dart-using-ffi

import 'dart:ffi' as ffi; // For FFI
import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:ffi/src/utf8.dart';

typedef get_key_func = ffi.Pointer<Utf8> Function(); // FFI fn signature
typedef GetKey = ffi.Pointer<Utf8> Function(); // Dart fn signature
final dylib = ffi.DynamicLibrary.open('lib/c_lib/lib.a');

final GetKey getKey =
    dylib.lookup<ffi.NativeFunction<get_key_func>>('GetKey').asFunction();

String getIv() {
  var addressOf = getKey();
  var res = addressOf.toDartString();
  // print(addressOf.cast<Uint8>().value);
  // print(addressOf.toDartString());
  print(res);
  return res;
}

@harishkthedeveloper
Copy link
Author

getIv() if called from other widgets is giving path error.

@dcharkes
Copy link
Collaborator

dcharkes commented Mar 3, 2022

final dylib = ffi.DynamicLibrary.open('lib/c_lib/lib.a');

This doesn't work, .a is a statically linked library, that requires using DynamicLibrary.process() or DynamicLibrary.executable(). Dynamically linked libraries are .dylib (MacOS, iOS), .dll (Windows), .so (Android, Linux).

@harishkthedeveloper
Copy link
Author

harishkthedeveloper commented Mar 4, 2022

@dcharkes Thanks for the reply
But whats the solution ? I am storing my encryption key in lib.a and i want to access the key in flutter widgets. is there any work around?

@dcharkes
Copy link
Collaborator

dcharkes commented Mar 4, 2022

Compile to a .dylib instead of .a. Or, if you're building a Flutter application (instead of Dart stand alone) link the .a statically into your Flutter application.

@dcharkes dcharkes transferred this issue from dart-archive/ffi Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants