Retriving app.currentUser to open Realm connection in another file / Flutter

Hi guys! I’m new here and in mongoDB / Flutter / Realm world.

I’m building an app with Flutter/Dart and Realm/MongoDB Atlas.

My app has a login with email/password and it is working perfectly when I call all the code bellow in the same file (loginPage.dart), after the user insert email and password:

const String _appId = 'xxxxx';
final AppConfiguration _appConfig = AppConfiguration(_appId);
final App app = App(_appConfig);

        var emailCred = Credentials.emailPassword(email, password);

        User currentUser = await app.logIn(emailCred);

        final realm = Realm(
          Configuration.flexibleSync(
            currentUser,
            [Dog.schema],
            syncErrorHandler: (SyncError error) {
              print("Error message ${error.message.toString()}");
            },
          ),
        );
        print('User: ${currentUser.id}');

        realm.subscriptions.update((mutableSubscriptions) {
          mutableSubscriptions.add(
              realm.all<Dog>());
        });

        realm.write(() {
          realm.add(Dog(ObjectId(), 12, 'Clifford3'));
        });

But, it is not working when I try to separated the code in other files like:

main.dart:

const String _appId = 'xxxxxx';
final AppConfiguration _appConfig = AppConfiguration(_appId);
final App app = App(_appConfig);

void main() {
  final ItemService service = ItemService();
  runApp(MyApp(
    service: service,
  ));
}

loginPage.dart:

      var emailCred = Credentials.emailPassword(email, password);
        User currentUser = await app.logIn(emailCred);

item_service_dart:

  openRealm() {

    final Configuration _config = Configuration.flexibleSync(
      app.currentUser,
      [Item.schema],
      syncErrorHandler: (SyncError error) {
        print("Error message ${error.message.toString()}");
      },
    );
    _realm = Realm(_config);
  }

The error bellow happens in openRealm function in “app.currentUser” that is not setted. How can I get the user data?

LateInitializationError: Field '_realm@702196172' has not been initialized.

Thanks!

Are you displaying the login page at all? There seems to be code missing and my guess is that at the time openRealm is called, the user hasn’t been authenticated yet. Would it be possible to upload the complete project somewhere so we can look at the full picture?

Hi @nirinchev . Thanks for your reply.

My git repo is GitHub - coffeelydev/shopping-list-v2: original code: https://github.com/markandeyp/flutter-shopping-list Maybe could help you to understand. There is not a login page. It will be in the future. Now I just call the an Auth function.

Now there is 2 errors:

  1. Error in realm initialization. I think I need to change something in main.dart
"LateInitializationError: Field '_realm@702196172' has not been initialized."
  1. Realm is not syncing with my database. I think I need to set something in my database.
[ERROR] Realm: Connection[1]: Session[1]: Error integrating bootstrap changesets: Failed to transform received changeset: Schema mismatch: 'Item' has primary key '_id', which is nullable on one side, but not the other.
flutter: Error message Bad changeset (DOWNLOAD)

I already created a schema but it is not working:

{
  "title": "Item",
  "bsonType": "object",
  "required": [
    "_id",
    "done",
    "text"
  ],
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "done": {
      "bsonType": "bool"
    },
    "text": {
      "bsonType": "string"
    }
  }
}

This happens because you haven’t set _realm yet when you call ItemService.getItems the first time.

When you mark a member a late in dart without an initializer, then it is your responsibility that it is assigned before you first use the variable.

Hence the

"LateInitializationError: Field '_realm@702196172' has not been initialized."

you see.

This is not related to realm

@Leozitus I have made a PR with a few fixes.

BTW: Be aware that you have committed your credentials in the repo!

Perfect, @Kasper_Nielsen1 ! Thanks a lot!
Yeah, I just keep my credentials to help you guys to find the error. Now I will remove it.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.