Generating Realm Object changes order of fields

I have a flutter object that when I generate the Realm object, the order of the fields in my object gets changed. Is that normal? It will be annoying if the order changes every time I generate. I cannot see a logical reason for the order it has chosen. As an example, this is the start of my object:

@RealmModel()
@MapTo("Appointment")
class _Appointment {
  @PrimaryKey()
  @MapTo("_id")
  ObjectId? id;
  @MapTo("user_id")
  String? userId;
  String? operativeId;
  String? jobNo1;
  int? jobEntryId;
  int? appointmentId;

and this is the start of the generated object

class Appointment extends _Appointment
with RealmEntity, RealmObjectBase, RealmObject {
static var _defaultsSet = false;

 Appointment(
    ObjectId? id, {
    String? appointmentNotes,
    String? userId,
    String? operativeId,
    String? jobNo1,
    int? jobEntryId,
    int? appointmentId,
    String? address,
    int? addressId,

It can be seen that appointmentNotes has moved to near the start of the generated object

I’m quite new to dart and would appreciate it if anyone has any advice on this because I can’t help feeling I’m missing something.
For example, is it normal that the generated Realm class has fields in a different order than my original private class? Without being able to have a constructor and named parameters, I have to create an object with the fields in the order they are in the generated class, and I’m worried that the order could change at any time since it seems to be a random order.

What is the importance of field order in a Realm object? (as it’s generated, not in the model definition)

Thank you for the reply. That may well be where I’m missing something obvious. As I see it, I cannot instantiate the class in my model because it is private to that file having been declared as _Appointment
So I have to instantiate Appointment which is the one in the Realm generated file and that does not have named parameters.

You have two options for models in Realm:

  • Create object models in the client application, and generate App Services schemas from them.
  • Create schemas in App Services, and generate Realm object models from them.

In general in development, the first option is the way to go. Just craft your object models in code and the server will take care of the rest when it syncs - the Atlas models will match your code. It’s the best option.

The second option has a section in the documentation Generate SDK Object Models but because you have complete model control in the SDK, it’s pretty simple just to default to the first option and create the models in code.

Most importantly, the order in which the fields appear in Atlas is generally unimportant. You can think of a Realm model (object) behaving much like a dictionary in regards to it’s properties. The order of properties is unimportant to the function of the model.

Thank you. I am using option 1 where my models are created in the app. So far I have 3 models and 2 of them are working fine. They are small models with only about 6 fields in them. It is the larger model that is giving me the problem. The extract I showed in my original message is just a few fields from the much larger real model. But it is only 43 simple fields with nothing nested. Just a primary key as shown above and then Strings, ints, bools and DateTime.

Ignoring my original question (which is still a problem for me), when I examine a document read back from the appointment collection, all the fields are duplicated. One with valid data and one with for example:

Appointment
_accessor: RealmCoreAccessor
_handle: RealmObjectHandle
_managedHashCode: 8095966396088600829
_realm: Realm
addVecoLink: false
addVecoLink: <not initialized>
address: <not initialized>
address: "1 Made up address , Somewhere"
addressId: <not initialized>
addressId: 30000001
allowTenantAppointmentTracking: -1
allowTenantAppointmentTracking: <not initialized>
appointmentCategory: <not initialized>
appointmentCategory: -1

It’s a good idea to keep questions limited to one topic, that way answers and discussions are short and on point instead of going on to a different issue.

If there’s more to the question about the order of the properties, per the original question, we can delve further into that. Otherwise it would be best to open another question about the duplicate property issue.

In the meantime, have you tried deleting the files and re-generating? Have you checked the Atlas console to see what the objects actually look like on the server (are there duplicate properties?)

Lastly, if you do post a separate question, be sure to include the actual code where the model in question is defined, and then what’s generated from that model. We can then take a look.

Thank you for all your help. I’ve raised a case now with MongoDB and supplied the source files, so I’ll wait for them to respond. If there is a solution, I’ll reply here.
I take your point about adding a different problem. I did so just in case it was caused by the same generation issue. To answer your questions. The documents synced to MongoDB, do look fine. I only see the duplicate fields in the VS Code debugger. I have done flutter clean and regenerated many times but it does not help.

Hi Jay. MongoDB are yet to get back to me (only been overnight) but I’ve been experimenting.

I found that if I make the fields in my model, nullable, then the generated class does make the fields named instead of positional. This means the order of the fields does not matter because I can instantiate the class with named parameters.

I’ll report back if MongoDB support have an answer to the duplicated fields in the debugger. My concern is that I cannot find a document if I try to find via the document. If I find via the document’s id, it is found.