mirror of
https://github.com/MAGICGrants/flutter_libsparkmobile.git
synced 2026-01-09 21:17:56 -05:00
Pass a List<int> keyData
This commit is contained in:
@@ -17,23 +17,33 @@ class FlutterLibsparkmobile {
|
||||
|
||||
// SparkMobileBindings methods:
|
||||
|
||||
Future<String> getAddress(String keyData, int index, int diversifier) {
|
||||
// Convert the Dart String to a Pointer<Char>.
|
||||
final keyDataPointer = keyData.toNativeUtf8().cast<Char>();
|
||||
Future<String> getAddress(
|
||||
List<int> keyData, int index, int diversifier) async {
|
||||
// Allocate space for the key data on the native heap.
|
||||
final keyDataPointer =
|
||||
malloc.allocate<Int>(keyData.length * sizeOf<Int32>());
|
||||
|
||||
// Call the native method.
|
||||
Pointer<Char> addressPointer =
|
||||
_bindings.getAddress(keyDataPointer, index, diversifier);
|
||||
// Copy the key data into the allocated space.
|
||||
for (int i = 0; i < keyData.length; i++) {
|
||||
keyDataPointer[i] = keyData[i];
|
||||
}
|
||||
|
||||
// Cast the Pointer<Char> to a Dart String.
|
||||
final addressString = addressPointer.cast<Utf8>().toDartString();
|
||||
// Call the native method with the pointer.
|
||||
final addressPointer = _bindings
|
||||
.getAddress(keyDataPointer, keyData.length, index, diversifier)
|
||||
.cast<Utf8>();
|
||||
|
||||
// It's a good idea to free the native string after use if the native method allocates memory.
|
||||
malloc.free(addressPointer);
|
||||
// Convert the Pointer<Utf8> to a Dart String.
|
||||
final addressString = addressPointer.toDartString();
|
||||
|
||||
// It's also important to free any pointers that were allocated to pass parameters.
|
||||
// Free the native heap allocated memory for the key data.
|
||||
malloc.free(keyDataPointer);
|
||||
|
||||
return Future.value(addressString);
|
||||
// The native side allocated memory for the returned address,
|
||||
// it needs to be freed after copying it to Dart-controlled memory.
|
||||
final String result = addressString;
|
||||
malloc.free(addressPointer);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,12 +23,14 @@ class SparkMobileBindings {
|
||||
: _lookup = lookup;
|
||||
|
||||
ffi.Pointer<ffi.Char> getAddress(
|
||||
ffi.Pointer<ffi.Char> keyData,
|
||||
ffi.Pointer<ffi.Int> keyData,
|
||||
int keyDataLength,
|
||||
int index,
|
||||
int diversifier,
|
||||
) {
|
||||
return _getAddress(
|
||||
keyData,
|
||||
keyDataLength,
|
||||
index,
|
||||
diversifier,
|
||||
);
|
||||
@@ -37,7 +39,7 @@ class SparkMobileBindings {
|
||||
late final _getAddressPtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Pointer<ffi.Char> Function(
|
||||
ffi.Pointer<ffi.Char>, ffi.Int, ffi.Int)>>('getAddress');
|
||||
ffi.Pointer<ffi.Int>, ffi.Int, ffi.Int, ffi.Int)>>('getAddress');
|
||||
late final _getAddress = _getAddressPtr.asFunction<
|
||||
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>, int, int)>();
|
||||
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Int>, int, int, int)>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user