You can use annotations to add functionality to properties in your Realm object models.
Required and Optional Properties
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 { () late ObjectId id; late String? maybeDescription; // optional value late double milesTravelled = 0; // 0 is default value () late String notInRealmModel; () late String make; ('wheels') // 'wheels' is property name in the RealmObject late int numberOfWheels; }
Default Field Values
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 { () late ObjectId id; late String? maybeDescription; // optional value late double milesTravelled = 0; // 0 is default value () late String notInRealmModel; () late String make; ('wheels') // 'wheels' is property name in the RealmObject late int numberOfWheels; }
llave primaria
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, andUuidtypes can be primary keys.Realm indexa automáticamente las claves primarias.
Las claves principales son nulas.
nullsolo puede ser la clave principal de un objeto en una colección.
class _Vehicle { () late ObjectId id; late String? maybeDescription; // optional value late double milesTravelled = 0; // 0 is default value () late String notInRealmModel; () late String make; ('wheels') // 'wheels' is property name in the RealmObject late int numberOfWheels; }
Asignar una propiedad o clase a un nombre diferente
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.
() ('naval_ship') class _Boat { () late ObjectId id; late String name; late int? maxKnots; late int? nauticalMiles; }
class _Vehicle { () late ObjectId id; late String? maybeDescription; // optional value late double milesTravelled = 0; // 0 is default value () late String notInRealmModel; () late String make; ('wheels') // 'wheels' is property name in the RealmObject late int numberOfWheels; }
Ignore Properties from Realm Schema
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 { () late ObjectId id; late String? maybeDescription; // optional value late double milesTravelled = 0; // 0 is default value () late String notInRealmModel; () late String make; ('wheels') // 'wheels' is property name in the RealmObject late int numberOfWheels; }
Propiedades del índice
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:
boolintStringObjectIdUuidDateTimeRealmValue
class _Vehicle { () late ObjectId id; late String? maybeDescription; // optional value late double milesTravelled = 0; // 0 is default value () late String notInRealmModel; () late String make; ('wheels') // 'wheels' is property name in the RealmObject late int numberOfWheels; }
Full-Text Search Indexes
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:
() class _Rug { () late ObjectId id; (RealmIndexType.fullText) late String pattern; (RealmIndexType.fullText) late String material; late int softness; }