There's no way to get around copying elements into FFI arrays.
for (var i = 0; i < listObject.length; i++) {
my_struct.states[i] = listObject[i];
}
This may seem inefficient, but consider that depending on the specialization of listObject
, the underlying memory layout of the data may differ significantly from the contiguous FFI layout, and so a type conversion sugar provided by Dart would likely also need to perform conversions on individual elements anyways (as opposed to just performing a single memcpy
under the hood).
One possibility for closing the convenience gap would be to define an extension method. For example:
extension FloatArrayFill<T> on ffi.Array<ffi.Float> {
void fillFromList(List<T> list) {
for (var i = 0; i < list.length; i++) {
this[i] = list[i] as double;
}
}
}
Usage:
my_struct.states.fillFromList(list);
Note that a separate extension method would be need to be defined for each ffi.Array<T>
specialization you want to do this for (Array<Uint32>
, Array<Double>
, Array<Bool>
, etc.).
This is due to the []
operator being implemented through a separate extension method for each of these type specializations internally.