Convert Uint8List to Pointer<Uint8>

1,597 views
Skip to first unread message

Kevin Wu

unread,
Mar 18, 2021, 1:04:36 AM3/18/21
to Flutter Development (flutter-dev)
I have a dart:typed_data Uint8List from reading a gzip file and decompressing it with the archive package. I need to get a dart:ffi Pointer<Uint8> so that I can pass the pointer to a C native library using FFI. How do I get the address of the Uin8List's buffer?

Kevin

Tim Sneath

unread,
Mar 18, 2021, 5:27:49 PM3/18/21
to Flutter Development (flutter-dev)
Try something like this:

extension Uint8ListBlobConversion on Uint8List {
  /// Allocates a pointer filled with the Uint8List data.
  Pointer<Uint8> allocatePointer() {
    final blob = calloc<Uint8>(length);
    final blobBytes = blob.asTypedList(length);
    blobBytes.setAll(0, this);
    return blob;
  }
}

Kevin Wu

unread,
Mar 18, 2021, 9:24:44 PM3/18/21
to Flutter Development (flutter-dev)
Hi Tim,

Thank you for your suggestion. I think it will work, but may involve copying the buffer produced by the archive package into the blob. I will settle for that for now. I found some relevant links on this topic:

Reply all
Reply to author
Forward
0 new messages