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!