Flutter Realm Error: Property type ObjectId not supported

355 views Asked by At

I'm trying to query my MongoDB database based on the primary key, which is _id and of type ObjectId.

I get the error:

The following RealmException was thrown building Builder:
Property type ObjectId not supported

when using the code:

ObjectId id = ObjectId.parse(retrievedId); //retrievedId is a String in the form of an ObjectId
var userData = realmServices.realm.query<UserSchema>(r"_id == $0", [id]);

I tried serializing with BSON but it kept giving me the same error. Also, using the query like:

String query = "_id == oid($id)";
var userData = realmServices.realm.query<UserSchema>(query);

but these solutions didn't work either, the query one giving me a Invalid syntax error.

What should I do?

EDIT

As suggested by @Jay, I tried implementing the 'find' method, which queries by primary key.

Using the code:

ObjectId id = ObjectId.parse(retrievedId);
print(id); //this looks like 'ObjectId("someobjectidhere")'
var userData = realmServices.realm.find<UserSchema>(id);

I manage to get the same error:

The following RealmException was thrown building Builder:
Property type ObjectId not supported

which points directly to this line: var userData = realmServices.realm.find<UserSchema>(id);

If id is converted to String instead of ObjectId, the error disappears, but, the queried data is always null.

So here comes my question, what am I doing wrong this time?

0

There are 0 answers