Why does Dart FFI generate an opaque class from this C struct?


I want to use the MAVLink C library to parse MAVLink packets in dart, but the Dart FFI generated mavlink_message_t is an opaque class, while other structs like mavlink_system_t are generated normally with all their attributes. What is the reason for this behaviour, and how can I fix it?

mavlink_message_t C struct

typedef struct __mavlink_message {
    uint16_t checksum;      ///< sent at end of packet
    uint8_t magic;          ///< protocol magic marker
    uint8_t len;            ///< Length of payload
    uint8_t incompat_flags; ///< flags that must be understood
    uint8_t compat_flags;   ///< flags that can be ignored if not understood
    uint8_t seq;            ///< Sequence of packet
    uint8_t sysid;          ///< ID of message sender system/aircraft
    uint8_t compid;         ///< ID of the message sender component
    uint32_t msgid:24;      ///< ID of message in payload
    uint8_t ck[2];          ///< incoming checksum bytes
    uint8_t signature[MAVLINK_SIGNATURE_BLOCK_LEN];
}) mavlink_message_t;

mavlink_system_t C struct

typedef struct __mavlink_system {
    uint8_t sysid;   ///< Used by the MAVLink message_xx_send() convenience function
    uint8_t compid;  ///< Used by the MAVLink message_xx_send() convenience function
}) mavlink_system_t;

FFI generated Dart classes

class mavlink_message_t extends ffi.Opaque {}

class mavlink_system_t extends ffi.Struct {
  external int sysid;

  external int compid;


Thanks to @Richard Heap ‘s comment, I found out the source of this issue was the msgid:24 field definition. Since it’s a bit field, which isn’t currently supported by Dart FFI, ffigen generates an opaque class definition from it. After removing the msgid:24 declaration, instead of ffi.Opaque, an ffi.Struct was generated

class mavlink_message_t extends ffi.Struct {
  external int checksum;

  external int magic;

  external int len;

  external int incompat_flags;

  external int compat_flags;

  external int seq;

  external int sysid;

  external int compid;

  external ffi.Array<ffi.Uint64> payload64;

  external ffi.Array<ffi.Uint8> ck;

  external ffi.Array<ffi.Uint8> signature;

This of course is not enough since I need all the fields, but there is a workaround for this which I’ll test.

Answered By – alielbashir

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *