Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To convert a vector within a 2D array into Dart/Flutter, you can use the following steps:

  1. Define the struct in Dart/Flutter to match the corresponding struct in C. For example, if the vector within the 2D array is of type int, the struct definition in Dart/Flutter would look like this:
import 'dart:ffi' as ffi;

class ExampleStruct extends ffi.Struct {
  @ffi.Array.multi([10, 10]) // 10x10 2D array
  late ffi.Array<ffi.Array<ffi.Int32>> vector;
}
  1. Use the ffi.Pointer class to obtain a pointer to the 2D array. This pointer can be passed to an FFI function in order to operate on the array.
// assuming the 2D array is named `myArray`:
var struct = ExampleStruct()
  ..vector = ffi.Array<ffi.Array<ffi.Int32>>.generate(
    10, // outer dimension
    (i) => ffi.Array<ffi.Int32>.generate(
        10, // inner dimension
        (j) => myArray[i][j],
    ),
);
var structPointer = struct.addressOf;
  1. Pass the structPointer to an FFI function to operate on the array.
// assuming an FFI function named `operateOnArray`:
operateOnArray(structPointer);