realm-dart
realm-dart copied to clipboard
Flutter class with @MapTo and late (i.e. a required property) leads to Flutter linter issue if json_annotation not imported
I think we may have found a bug related to 'required' fields (i.e. making a property late) that also make use of @MapTo:
To recreate:
1. Create the Flutter class below:
@RealmModel()
class _Car {
@MapTo("name")
late String make;
}
2. run flutter pub run realm generate
The flutter pub run realm generate is successful with no errors reported in terminal, however the Flutter linter then reports an error. In order to remove the Flutter 'linter' error, you have to import https://pub.dev/packages/json_annotation package at the top of the file (in this case the _Car class/model file which contains the class above)... Is this expected behaviour and the json_annotation import is necessary (perhaps to support @MapTo("name")) ?
In the above example, if we make the make property nullable and remove late (see below class example), there are no Flutter linter issues and no need to import the json_annotation package... This is however odd as @MapTo is in use and no need to import the json_annotation.
@RealmModel()
class _Car {
@MapTo("name")
String? make;
}
FYI ive checked https://www.mongodb.com/docs/realm/sdk/flutter/realm-database/define-realm-object-schema/ and can not find any references to use the json_annotation package when implementing @MapTo...
Hope this helps and interested to know your feedback.
That should not be needed. As an example the rudimentary flexible sync sample uses @MapTo to rename id to _id without the need to import to json_annotation.dart.
What linter violation are you seeing, and could you share the full file containing your model, or perhaps a sample reproduction project?
@nielsenko apologies we have not come back yet - will make a reply with a full example shortly.
@nielsenko no longer able to replicate. Thanks for your help on this and will close.