RealmValue.from constructor

RealmValue.from(
  1. Object? object
)

Constructs a RealmValue from an arbitrary object. Collections will be converted recursively as long as all their values are compatible.

Throws ArgumentError if any of the values inside the graph cannot be stored in a RealmValue.

Implementation

factory RealmValue.from(Object? object) {
  return switch (object) {
    null => RealmValue.nullValue(),
    bool b => RealmValue.bool(b),
    String text => RealmValue.string(text),
    int i => RealmValue.int(i),
    double d => RealmValue.double(d),
    RealmObjectMarker o => RealmValue.realmObject(o),
    DateTime d => RealmValue.dateTime(d),
    ObjectId id => RealmValue.objectId(id),
    Decimal128 decimal => RealmValue.decimal128(decimal),
    Uuid uuid => RealmValue.uuid(uuid),
    Uint8List binary => RealmValue.binary(binary),
    Map<String, RealmValue> d => RealmValue.map(d),
    Map<String, dynamic> d => RealmValue.map(d.map((key, value) => MapEntry(key, RealmValue.from(value)))),
    List<RealmValue> l => RealmValue.list(l),
    List<dynamic> l => RealmValue.list(l.map((o) => RealmValue.from(o)).toList()),
    Iterable<RealmValue> i => RealmValue.list(i.toList()),
    Iterable<dynamic> i => RealmValue.list(i.map((o) => RealmValue.from(o)).toList()),
    _ => throw ArgumentError.value(object.runtimeType, 'object', 'Unsupported type'),
  };
}