Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot include a Realm class into another Realm class #1824

Open
HeavySnowJakarta opened this issue Jan 19, 2025 · 4 comments
Open

Cannot include a Realm class into another Realm class #1824

HeavySnowJakarta opened this issue Jan 19, 2025 · 4 comments

Comments

@HeavySnowJakarta
Copy link

What happened?

I wonder how to include custom objects on Realm class which I could found nowhere on the doc.

Repro steps

@RealmModel()
class _CustomField{
  // The tag if the custom field is a single string or a double string.
  late final String type;
  // The single field.
  late final String? single;
  // The double fields.
  late final String? our;
  late final String? their;
}

The codes above runs with no error. Then I tried

@RealmModel()
class _CustomField{
  // The tag if the custom field is a single string or a double string.
  late final String type;
  // The single field.
  late final String? single;
  // The double fields.
  late final String? our;
  late final String? their;
}

@RealmModel()
class _CustomFields{
  late List<CustomField> customFields;
}

Now it complains List<CustomField> is not a realm model type. Even without List the issue exists.

ChatGPT told me to add a label @RealmEmbedded but it doesn't seem to work. Dart language server complains Undefined name 'RealmEmbedded' used as an annotation. Try defining the name or importing it from another library. The script complains Could not resolve annotation for 'class _CustomField'.

What's more: What would it happen if I try to use an abstract class object as a data field of a Realm object? How to express things like Rust enums or C/C++ unions?

Version

Dart 3.5.1 Flutter 3.24.1

What Atlas Services are you using?

Local Database only

What type of application is this?

Flutter Application

Client OS and version

Windows 11 21H2

Code snippets

No response

Stacktrace of the exception/crash you're getting

Relevant log output

@nielsenko
Copy link
Contributor

Use List< _CustomField> instead.

@nielsenko
Copy link
Contributor

Regarding embedded types, ChatGPT is wrong, the correct annotation is @RealmModel(ObjectType.embeddedObject).

@nielsenko
Copy link
Contributor

Realm doesn't support enums natively, so you will need to map them to either a string or an int. Something like:

import 'package:realm/realm.dart';

part 'main.realm.dart';

@RealmModel()
class _Car {
  @MapTo("type")
  late String _type;
  CustomType get type => CustomType.values.byName(_type);
  set type(final CustomType value) => _type = value.name;
}

enum CustomType { enabled, disabled }

final realm = Realm(Configuration.local([Car.schema]));

void main() {
  realm.write(() {
    // best bet right now ..
    realm.add(Car(CustomType.enabled.name));
    // or
    realm.add(Car('')..type = CustomType.enabled);
  });
}

@nielsenko
Copy link
Contributor

As you seem to be a new user, I should probably end this by pointing out that all the Atlas Device Sync (aka Realm) SDKs are deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants