copyUint8ListFromOwnedFfiPtr function

Uint8List? copyUint8ListFromOwnedFfiPtr(
  1. Pointer<Uint8> data,
  2. int length
)

From an owned pointer allocated in native code, copy the data into the Dart VM Heap as a Uint8List and then immediately free the owned ffi pointer.

Implementation

Uint8List? copyUint8ListFromOwnedFfiPtr(Pointer<Uint8> data, int length) {
  if (data == nullptr) {
    return null;
  }
  // Native code hands over a buffer even for an empty payload (`malloc(0)` is
  // not null on most platforms), so it still has to be freed here.
  if (length == 0) {
    malloc.free(data);
    return null;
  }

  final Uint8List out = Uint8List.fromList(data.asTypedList(length));
  malloc.free(data);
  return out;
}