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

Realm generator rejects const values saying initialization must be const #1606

Closed
derrickgw opened this issue Mar 25, 2024 · 8 comments · Fixed by #1607
Closed

Realm generator rejects const values saying initialization must be const #1606

derrickgw opened this issue Mar 25, 2024 · 8 comments · Fixed by #1607

Comments

@derrickgw
Copy link

derrickgw commented Mar 25, 2024

What happened?

Using constant classes or negative numbers used to work for initialization. When upgrading from 1.6 to 2.0, this no longer works:


Field initializers must be constant

in: package:sustain/model/data/picture.dart:19:14
    ╷
16  │ @RealmModel()
17  │ class _Example {
    │       ━━━━━━━━ in realm model for 'Example'
... │
19  │   double y = -1;
    │              ^^ Must be const
    ╵
Ensure the default value for field "y" is const

Repro steps

Create a class with integer or float field and try to initialize it to a constant negative value.
Presumably this would work (or rather fail to work) with any other class that provides a const constructor.

Version

2.0.0

What Atlas Services are you using?

Local Database only

What type of application is this?

Flutter Application

Client OS and version

Linux

Code snippets

@RealmModel()
class _Example {
  double x = 0;
  double y = -1;
  int z = -1;
}

Stacktrace of the exception/crash you're getting

No response

Relevant log output

[SEVERE] realm:realm_generator on lib/model/data/picture.dart:

Field initializers must be constant

in: package:sustain/model/data/picture.dart:19:14
    ╷
16  │ @RealmModel()
17  │ class _Example {
    │       ━━━━━━━━ in realm model for 'Example'
... │
19  │   double y = -1;
    │              ^^ Must be const
    ╵
Ensure the default value for field "y" is const

Copy link

sync-by-unito bot commented Mar 25, 2024

➤ PM Bot commented:

Jira ticket: RDART-992

@Bilonik
Copy link

Bilonik commented Mar 26, 2024

Hi,

Same issue here, any workaround?

@nirinchev
Copy link
Member

We're aware of this and will address it with a future release. Currently, the only known mitigation is to downgrade to 1.6. I'm sorry for the regression.

@nielsenko nielsenko self-assigned this Mar 27, 2024
@nielsenko
Copy link
Contributor

Apparently -1 is not a literal to the Dart analyzer

@derrickgw
Copy link
Author

derrickgw commented Mar 27, 2024 via email

@derrickgw
Copy link
Author

derrickgw commented Mar 30, 2024

That looks like a great improvement. Unfortunately there are still some cases that cause false errors. Here is an example:

import 'package:dart_numerics/dart_numerics.dart' as numerics;
const int myconst = 1;
@RealmModel(ObjectType.embeddedObject)
class $Test {
  ObjectId id = ObjectId();
  int value1 = myconst;
  int value2 = numerics.int64MinValue;
}

If I comment out the new const test, this generates legal dart.

@nielsenko
Copy link
Contributor

@derrickgw Thank you for your observation. I have created #1612 to handle value1 and value2.

But this const check was added explicitly to disallow ObjectId id = ObjectId(), so that is still not allowed.

Consider:

@RealmModel()
class _Stuff {
  ObjectId id = ObjectId();
}

Which is legal with realm_generator 1.9.0. If you look at the generated class, you will find that is illegal because it lifts the initializer expression and uses it as the default value of the optional parameter id on the generated constructor.

Stuff({
    ObjectId id = ObjectId(), // <-- this is not valid Dart, as default values must be const
  }) {

@derrickgw
Copy link
Author

Wow. That was super fast. Thanks.

I see now that ObjectId has a constructor body, and calls DateTime.now(), so it can't really become a const class.

The previous database code generator I migrated from had a different paradigm that generated a Schema class instead of a child class, which allowed non-const default values, but had other limitations.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants