Docs Menu

Docs HomeRealm

Define a Realm Object Schema - Flutter SDK

On this page

  • Create Model
  • Import Realm
  • Create Generated File Part Directive
  • Create RealmModel
  • Generate RealmObject
  • Use RealmObject in Application
  • Using Schemas with Device Sync
  • Supported Data Types
  • Property Annotations
  • Define Relationship Properties
  • Map Realm Model to Different Name
  • Generate the RealmObject

An object schema is a configuration object that defines the properties and relationships of a Realm object. Realm client applications define object schemas with the native class implementation in their respective language using the Object Schema.

Object schemas specify constraints on object properties such as the data type of each property and whether or not a property is required. Schemas can also define relationships between object types in a realm.

1

Import the Realm SDK package at the top of your file.

2

Add a part directive to include the RealmObject file that you generate in step 4 in the same package as the file you're currently working on.

schemas.dart
part 'schemas.g.dart';
3

Create the model for your Realm schema. You must include the annotation RealmModel at the top of the class definition.

You'll use the RealmModel to generate the public RealmObject used throughout the application in step 4.

You can make the model private or public. We recommend making the all models private and defining them in a single file. Prepend the class name with an underscore (_) to make it private.

If you need to define your schema across multiple files, you can make the RealmModel public. Prepend the name with a dollar sign ($) to make the model public. You must do this to generate the RealmObject from the RealmModel, as described in step 4.

Add fields to the RealmModel. You can add all supported data types. Include additional behavior using property annotations.

schemas.dart
@RealmModel()
class _Car {
@PrimaryKey()
late ObjectId id;
late String make;
late String? model;
late int? miles;
}
4

Generate the RealmObject, which you'll use in your application:

This command generates the file in the same directory as your model file. It has the name you specified in the part directive of step 2.

Tip

Track the generated file

Track the generated file in your version control system, such as git.

Example

File structure after generating model

.
├── schemas.dart
├── schemas.g.dart // newly generated file
├── myapp.dart
└── ...rest of application
5

Use the RealmObject that you generated in the previous step in your application. Since you included the generated file as part of the same package where you defined the RealmModel in step 2, access the RealmObject by importing the file with the RealmModel.

myapp.dart
import './schemas.dart';
final hondaCivic = Car(ObjectId(), 'Honda', model: 'Civic', miles: 99);

An App Services Schema is a list of valid object schemas that each define an object type that an App may persist. All synced objects in a realm must conform to the App Services Schema.

Client applications provide a Object Schema when they open a realm. If a realm already contains data, then it already has a schema, and when it is opened, Realm Database validates the schema on the client against the existing schema.

You can define App Services Schemas in the following ways:

  • Automatically with the Object Schema if development mode is enabled.

  • Explicitly define the App Services Schema with App Services.

In your schema you must use the MapTo("_id") annotation with your primary key in the RealmModel to successfully sync your Object Schema with App Services.

@RealmModel()
class _SyncSchema {
@PrimaryKey()
@MapTo("_id")
late ObjectId id;
// ... other properties
}

For further information on defining your schema and which of these approaches you should consider for your application, refer to the Create a Realm Schema documentation.

Realm schemas support many Dart-language data types, in addition to some Realm-specific types. For a comprehensive reference of all supported data types, refer to Data Types.

Use annotations to add functionality to properties in your Realm object models. You can use annotations for things like marking a property as nullable, setting a primary key, ignoring a property, and more. To learn more about the available property annotations, refer to Property Annotations.

You can define relationships between Realm objects in your schema. The Realm Flutter SDK supports to-one relationships, to-many relationships, inverse relationships, and embedding objects within other objects. To learn more about how to define relationships in your Realm object schema, refer to Relationships.

You can use the MapTo annotation to map a Realm object model to a different model name in Realm Database. This can be useful in the following scenarios:

  • Working with one realm from different Realm SDKs

  • Migrating Realm object models

  • Adhering to certain code style conventions

If you're using Atlas Device Sync, the name that you map to corresponds with the App Services Schema name.

@RealmModel()
@MapTo('naval_ship')
class _Boat {
@PrimaryKey()
late ObjectId id;
late String name;
late int? maxKnots;
late int? nauticalMiles;
}

Once you've completed your Realm model, you must generate the RealmObject class to use it in your application.

Run the following command to generate RealmObjects:

Running this creates a public class in a new file in the directory where you defined the RealmModel class per the Create Model section.

The generated file has the same base name as the file with your RealmModel, ending with .g.dart. For example if the file with your RealmModel is named schemas.dart, the generated file will be schemas.g.dart.

Note

Remember to include the generated file in a part directive in your RealmModel definition file.

schemas.dart
// ...import packages
part 'schemas.g.dart';
@RealmModel()
// ...model definition

If you'd like to watch your data models to generate RealmObjects whenever there's a change, include the --watch flag in your command.

To clean the generator caches, include the --clean flag in your command. Cleaning the generator cache can be useful when debugging.

←  Model Data - Flutter SDKData Types - Flutter SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.