Dynamic Types

Dart's dynamic is a special type that can hold any type of value. Although it is possible to return a dynamic to Dart in the form of DartAbi, it is preferable to return an enum instead that has a variant for each type you want to support.


Let's say you have a struct that can hold either a u32 or a String and some other fields (in a significantly worse design):

struct MyStruct {
    a: Optional<u32>,
    b: Optional<String>,

struct DataStruct {
    msg:  String,
    data: MyStruct,

You can define an enum in Rust to represent this:

enum MyEnum {

And then you can define a struct that holds this enum:

struct MyStruct {
    msg:  String,
    data: MyEnum,

Returning dynamics

Aside from DartOpaque, you may also return a dynamic type to Dart by specifing the return type as DartAbi. DartAbi is the umbrella type for all C-representable Dart values, which can be obtained from Rust types that implement IntoDart.

pub fn return_dynamic() -> DartAbi {
        format!("Hello there!").into_dart()
final dynamic values = await api.returnDynamic();
assert(values is List<dynamic>);
assert(values[0] == null);
assert(values[1] == 0);
assert(values[2] == "Hello there!");

DartAbi is not supported as parameters, and structs that transitively include them may not be used in parameter positions either. If you only care about accepting or returning an opaque Dart object without interacting with it, consider DartOpaque.

This type is meant to be used only as an esacpe hatch, if your data cannot be expressed as either a fixed struct or enum.