In flexible sync read code is working but write code is not working

Hey, we are building an App on flutter, dart and realm. And we are trying to sync the data to the database using Flexible sync. But we are not able to progress further as There are no logs generated on the backend or on the Flutter dev tools for what’s happening.
The thread just halts with no error.
The transaction is not being started and no further code execution takes place,
the app freezes when we try to start the write transaction
Also the reading is done but gets stuck while writing.
Here is my write code.

userProfile adduserProfile(userProfile newuserProfile) {
    final userQuery = _realm.query<userProfile>("_id == \$0", [newuserProfile.id]);
    userProfile? userProfile;
    _realm.subscriptions.update((mutableSubscriptions) {
      mutableSubscriptions.add<userProfile>(userQuery, name: "add-user-query", update: true);
      userProfile = _realm.write((){
        return _realm.add(newuserProfile);
    });
    return userProfile!;
    return _realm.write(() {
       return _realm.add(newuserProfile);
     });
  }

Hm… this looks like a bug on our end that we don’t throw the correct error here. The issue is you’re starting a transaction inside the subscription update block. That is not allowed. Can you move the write outside of the update block and see if that resolves the issue.

1 Like

Thanks Nikola, As suggested we tried the following code

userProfile adduserProfile(userProfile newuserProfile) {
    final userQuery = _realm.query<userProfile>("_id == \$0", [newuserProfile.id]);
    userProfile? userProfile;
    _realm.subscriptions.update((mutableSubscriptions) {
      mutableSubscriptions.add<userProfile>(userQuery, name: "add-user-query", update: true);
    });
    return userProfile!;
    return _realm.write(() {
       return _realm.add(newuserProfile);
     });
  }

and getting this error
flutter: [ERROR] Realm: SyncError message: error category: SyncErrorCategory.unknown code: 1011

That sounds like a server error - can you try checking your server logs to see if there’s anything in there that may point us to what the issue is?