We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
The official flutter tutorial on C/C++ interop through ffi only touches on calling a C++ function and getting a single return value.
What if I have a data buffer created on C/C++ side, but want to deliver to dart/flutter-side to show using the natural iteration syntax?
I'm testing Dart's FFI by trying to have safe memory deallocation from Dart to C/C++. The test reuses the official struct sample .
I could get the Array as a dart Pointer, but it's unclear to me how to iterate the array as a collection easily.
Pointer
I'm implementing a Dart-side C array binding like this:
In struct.h
struct.h
struct Array { int* array; int len; };
and a pair of simple allocation/deallocation test functions:
struct Array* get_array(); int del_array(struct Array* arr);
Then on Dart side in structs.dart:
structs.dart
typedef get_array_func = Pointer<Array> Function(); typedef del_array_func = void Function(int arrAddress); ... final getArrayPointer = dylib.lookup<NativeFunction<get_array_func>>('get_array'); final getArray = getArrayPointer.asFunction<get_array_func>(); final arrayPointer = getArray(); final array = arrayPointer.ref.array; print('array.array: $array');
This gives me the print out
array.array: Pointer<Int32>: address=0x7fb0a5900000
Can I convert the array pointer to a List easily? Something like:
List
final array = arrayPointer.ref.array.toList(); array.forEach(idx, elem) => print("array[$idx]: $elem");
The text was updated successfully, but these errors were encountered:
So asTypedList() is the answer
asTypedList()
final arrReal = array.asTypedList(10); arrReal.forEach((idx, elem) => print("array[$idx]: $elem"));
Sorry, something went wrong.
No branches or pull requests
The official flutter tutorial on C/C++ interop through ffi only touches on calling a C++ function and getting a single return value.
Goal
What if I have a data buffer created on C/C++ side, but want to deliver to dart/flutter-side to show using the natural iteration syntax?
Problem
I'm testing Dart's FFI by trying to have safe memory deallocation from Dart to C/C++. The test reuses the official struct sample .
I could get the Array as a dart
Pointer
, but it's unclear to me how to iterate the array as a collection easily.Code
I'm implementing a Dart-side C array binding like this:
In
struct.h
and a pair of simple allocation/deallocation test functions:
Then on Dart side in
structs.dart
:This gives me the print out
Question
Can I convert the array pointer to a
List
easily? Something like:The text was updated successfully, but these errors were encountered: