Adding a Property to Realm Model Causing RealmException

In the documentation for Realm Flutter, in no uncertain terms, it says: “Realm can automatically migrate added and deleted properties. You must update the schema version when you make these changes.” (https://www.mongodb.com/docs/realm/sdk/flutter/realm-database/model-data/update-realm-object-schema/#add-a-property).

I performed the following:

  1. Added the testProp field below:
@RealmModel()
class _SavedItem {
  @PrimaryKey()
  late String key;
  @Indexed()
  late String type;
  late int itemId;
  int trackingId = 0;
  late String title;
  late String? image;
  late int? duration;
  late String? subtitle;
  late String? data;
  List<int> topicIds = [];
  List<String> topicNames = [];
  late String? testProp;
}
  1. Ran dart run realm generate
  2. Incremented the realmSchemaVersion in the code below

final realm = Realm(Configuration.local( [SavedItem.schema], schemaVersion: realmSchemaVersion, migrationCallback: (migration, oldSchemaVersion) {}, ));

Yet when I reopened my app, I was greeted with a RealmException, as shown below:

Is there anywhere I can check to see if I’m doing anything wrong? If I’m not doing anything wrong, why is the automatic migration which the documentation described not happening??

What you’re doing should be correct. Are you certain that realmSchemaVersion has been increased correctly? What was the previous version and what is the new one? Can you check if the migration callback is invoked at all - either by setting a breakpoint or logging a message? Another common mistake that you can double check for is opening the Realm at a different location with the old schema version - sometimes people will have multiple screens where they open the Realm file and they’ll forget to update the schema version in all of them.

realmSchemaVersion was 5 before and now is 6, I’ve even updated it to 7 to see if that triggers anything to no avail.

I put a breakpoint in migrationCallback and nothing happens! We never even get inside the migrationCallback

I can guarantee that I’m incrementing realmSchemaVersion variable directly, there’s no other realmSchemaVersion variable.