Do Realm object schema classes have to be in the same Dart file?

I’m using Flutter Realm SDK. I have two Dart classes that are used as Realm object schema. When these two classes are placed in two different Dart files generation gives error like below.

6 │ @RealmModel()
7 │ class _Account {
  │       ━━━━━━━━ in realm model for 'Account'
8 │   late _Person? person;
  │        ^^^^^^^^ _Person? is not a realm model type

_Person is defined in person.dart which is imported here.

But when placed within the same file generation succeed. Does that mean all Realm object schema classes have to be placed in the same Dart file?

Models can be in different files if they don’t reference each other. If you need to reference a model from another file make sure to use $ sign for the model class name instead of underscore sign.
So in your example it will be
$Person {

and then in another file
class _Account {
late $Person? person;

In general prefer using underscore for the models. This hides your model classes and only the generated Realm classes are visible throughout the application. This means prefer having models that reference each other in the same library.


1 Like

I stumbled on the same thing as I’m trying out Realm Flutter SDK.

I would like to hide my model classes but:

  1. Having all 13 models in the same file is not verbose.
  2. Some models might be shared and I prefer to the models in different files.

It would be nice if there was a way to hide models but still be able to use different files.
I’m VERY new to Dart but I’ve been contemplating if this can’t be accomplished with part/part of.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.