Docs Menu

Docs HomeAtlas Device SDK

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 a Model or Class to a Different Name
  • Generate the RealmObject
  • Define an Asymmetric Object

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

Note

Class names are limited to a maximum of 57 UTF-8 characters.

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 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 or property to a different stored name in Realm. This can be useful in the following scenarios. For example:

  • To make it easier to work across platforms where naming conventions differ. For example, if your Device Sync schema property names use snake case, while your project uses camel case.

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

  • To support multiple model classes with the same name in different packages.

  • To use a class name that is longer than the 57-character limit enforced by Realm.

If you're using Atlas Device Sync, the name that you specify in the MapTo annotation is used as the persisted App Services Schema name.

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.

New in version 1.5.0.

Asymmetric objects require Flexible Sync. To define an asymmetric object, pass ObjectType.asymmetricObject to @RealmModel().

@RealmModel(ObjectType.asymmetricObject)
class _WeatherSensor {
@PrimaryKey()
@MapTo("_id")
late ObjectId id;
late String deviceId;
late double modtemperatureInFahrenheitel;
late double barometricPressureInHg;
late double windSpeedInMph;
}

In Flutter SDK versions 1.5.0 and earlier, you cannot link from asymmetricObject types to RealmObjects. In SDK versions 1.6.0 and later, asymmetricObject types can link to RealmObjects in addition to embedded object types.

Note

Attempting to Read Asymmetric Objects

Asymmetric objects can't be read. If you attempt to query an asymmetric object, you will get the following error: "Error: You can't query an asymmetric class.".

To learn more about Data Ingest, refer to Stream Data to Atlas.

←  Model Data - Flutter SDKData Types - Flutter SDK →