Docs Menu
Docs Home
/ /
モデルデータ

プロパティ注釈 - Flutter SDK

注釈 を使用して、Realm オブジェクトモデルのプロパティに機能を追加できます。

Dartでは、値の型は暗黙的に null 以外ですが、 を追加することで任意(null 可能)にすることができます。 。プロパティをオプションにするには、? を含めます。

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

組み込みの言語機能を使用して、プロパティにデフォルト値を割り当てることができます。 プロパティ宣言でデフォルト値を割り当てます。

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

PrimaryKey 注釈はプライマリキープロパティを示します。プライマリキーは、 邦土内のオブジェクトの一意の識別子です。同じ型の他のオブジェクトは、オブジェクトのプライマリキーを共有できません。

プライマリキーの重要な要素:

  • Realm にオブジェクトを追加した後は、プライマリキーを変更できません。

  • RealmModel 内の 1 つのプロパティにのみプライマリキーを追加します。

  • プライマリキーになることができるのは、 StringintObjectIdUuidタイプのみです。

  • Realm はプライマリキーを自動的にインデックス化します。

  • プライマリキーは null 可能です。 nullは、コレクション内の 1 つのオブジェクトのプライマリキーのみにすることができます。

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

MapTo 注釈は、モデルまたはプロパティを別の名前で永続化する必要があることを示します。コード スタイルの規則が異なる可能性がある異なるバインディングでRealmを開く場合に便利です。(例: )。

  • 命名規則が異なるプラットフォーム間での操作を容易にします。 たとえば、Device Sync スキーマのプロパティ名がスニペットのケースを使用する場合、プロジェクトはキャメルケースを使用します。

  • 移行を強制せずにクラスまたはフィールド名を変更します。

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

のプロパティに [無視] の注釈を追加すると、RealmModel 邦土オブジェクトジェネレーターは スキーマにプロパティを含めないか、RealmObject 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;
}

フィールドにインデックスを作成するには、インデックス付き注釈を追加します。インデックスを使用すると、書き込み時間が若干遅くなり、ストレージとメモリのオーバーヘッドが増加する可能コストにして、一部のクエリを大幅に高速化できます。Realm はインデックスをディスクに保存するため、 邦土ファイルが大きくなります。各インデックスエントリは最小で 12 バイトです。インデックスは null にすることができます。

次のデータ型でインデックスを作成できます。

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

標準インデックスに加えて、Realm は string プロパティの全文検索(FTS)インデックスもサポートしています。 標準インデックスの有無にかかわらず string フィールドをクエリできますが、FTS インデックスを使用すると複数の単語とフレーズを検索し、その他を除外できます。

FTS インデックスのクエリの詳細については、「全文検索でのフィルタリング 」を参照してください。

プロパティに FTSインデックスを作成するには、 @Indexed アノテーションを使用し、RealmIndexTypefullText として指定します。これにより、プロパティに対する全文クエリが可能になります。次の例では、パターンと具体的なプロパティを FTS 注釈でマークします。

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

戻る

関係

項目一覧