Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
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.

Aspectos importantes de las claves primarias:

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

  • Solo agregue una clave principal a una propiedad en un RealmModel.

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

  • Realm indexa automáticamente las claves primarias.

  • Las claves principales son nulas. null solo puede ser la clave principal de un objeto en una colección.

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 MapTo indica que un modelo o propiedad debe persistir con un nombre diferente. Resulta útil al abrir un dominio con diferentes enlaces, donde las convenciones de estilo de código pueden variar. Por ejemplo:

  • 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;
}

Si agrega la anotación Ignorado a una propiedad en RealmModel su, el generador de objetos de reino no incluye la propiedad en el RealmObject esquema ni la persiste en Reino.

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;
}

Además de los índices estándar, Realm también admite índices de búsqueda de texto completo (FTS) en propiedades de cadena. Si bien se puede consultar un campo de cadena con o sin un índice estándar, un índice FTS permite buscar múltiples palabras y frases, excluyendo otras.

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

Relaciones

En esta página