Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
Datos del modelo

Anotaciones de propiedad - Flutter SDK

You can use annotations to add functionality to properties in your Realm object models.

En Dart, los tipos de valor son implícitamente no nulos, pero pueden hacerse opcionales (nulos) añadiendo ?. Incluir ? Hacer que las propiedades sean opcionales.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

Puedes usar las funcionalidades incorporadas del lenguaje para asignar un valor por defecto a una propiedad. Asigna un valor por defecto en la declaración de la propiedad.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

La anotación PrimaryKey indica una propiedad de clave primaria. La clave primaria es un identificador único para un objeto en un realm. No puede haber otros objetos del mismo tipo que compartan la clave primaria de un objeto.

Important aspects of primary keys:

  • No se puede cambiar una llave primaria después de agregar un objeto a un realm.

  • Only add a primary key to one property in a RealmModel.

  • Only String, int, ObjectId, and Uuid types can be primary keys.

  • Realm automatically indexes primary keys.

  • Primary keys are nullable. null can only be the primary key of one object in a collection.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

The MapTo annotation indicates that a model or property should be persisted under a different name. It's useful when opening a Realm across different bindings where code style conventions can differ. For example:

  • Para facilitar el trabajo entre plataformas donde las convenciones de nomenclatura difieren. Por ejemplo, si los nombres de las propiedades del esquema de Device Sync usan snake case, mientras que tu proyecto usa camel case.

  • To change a class or field name without forcing a migration.

@RealmModel()
@MapTo('naval_ship')
class _Boat {
@PrimaryKey()
late ObjectId id;
late String name;
late int? maxKnots;
late int? nauticalMiles;
}
class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

If you add the Ignored annotation to a property in your RealmModel, the realm object generator doesn't include the property in the RealmObject schema or persist it to Realm.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

Add the Indexed annotation to create an index on the field. Indexes can greatly speed up some queries at the cost of slightly slower write times and additional storage and memory overhead. Realm stores indexes on disk, which makes your realm files larger. Each index entry is a minimum of 12 bytes. Indexes can be nullable.

Se pueden indexar los siguientes tipos de datos:

  • bool

  • int

  • String

  • ObjectId

  • Uuid

  • DateTime

  • RealmValue

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

In addition to standard indexes, Realm also supports Full-Text Search (FTS) indexes on string properties. While you can query a string field with or without a standard index, an FTS index enables searching for multiple words and phrases and excluding others.

For more information on querying FTS indexes, see Filter with Full-Text Search.

To create an FTS index on a property, use the @Indexed annotation and specify the RealmIndexType as fullText. This enables full-text queries on the property. In the following example, we mark the pattern and material properties with the FTS annotation:

@RealmModel()
class _Rug {
@PrimaryKey()
late ObjectId id;
@Indexed(RealmIndexType.fullText)
late String pattern;
@Indexed(RealmIndexType.fullText)
late String material;
late int softness;
}

Volver

relación

En esta página