-
Notifications
You must be signed in to change notification settings - Fork 94
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
Comments
➤ PM Bot commented: Jira ticket: RDART-992 |
Hi, Same issue here, any workaround? |
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. |
Apparently |
True, but even if it was, checking for literals is not a good solution.
Const variables are legal initializers, but are not literals.
…On Wed, Mar 27, 2024, 04:51 Kasper Overgård Nielsen < ***@***.***> wrote:
Apparently -1 is not a literal to the Dart analyzer
—
Reply to this email directly, view it on GitHub
<#1606 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGJZQCOKM3EMHJKIIPTR4Q3Y2KJALAVCNFSM6AAAAABFHRN6JSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRSGQ3DEMBZGA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
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. |
@derrickgw Thank you for your observation. I have created #1612 to handle But this const check was added explicitly to disallow Consider: @RealmModel()
class _Stuff {
ObjectId id = ObjectId();
} Which is legal with Stuff({
ObjectId id = ObjectId(), // <-- this is not valid Dart, as default values must be const
}) { |
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. |
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:
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
Stacktrace of the exception/crash you're getting
No response
Relevant log output
The text was updated successfully, but these errors were encountered: