my schema.dart looks like this (simplified):
@RealmModel()
class _MainObj {
@PrimaryKey()
@MapTo('_id')
late ObjectId id;
@MapTo('name')
late String name;
@MapTo('description')
late String? description;
@MapTo('embedded_obj'')
late List<_EmbeddedObj> embeddedObj;
}
@RealmModel(ObjectType.embeddedObject)
class _EmbeddedObj {
@MapTo('embedded_obj2'')
late _EmbeddedObj2 embeddedObj2;
@MapTo('id_in_another_collection'')
late ObjectId anotherCollectionId;
}
@RealmModel(ObjectType.embeddedObject)
class _EmbeddedObj2 {
@MapTo('name')
late String name;
}
@RealmModel()
class _AnotherCollection {
@PrimaryKey()
@MapTo('_id')
late ObjectId id;
@MapTo('name')
late String name;
@MapTo('embedded_obj2')
late _EmbeddedObj2? embeddedObj2;
}
I have a top level StatefulWidget doing a realm.all for MainObj and doing a ListView.builder() on the results, which is working fine (based on the to_do flutter flexible template)…
@override
Widget build(BuildContext context) {
final realmServices = Provider.of<RealmServices>(context);
return Stack(
children: [
Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
child: StreamBuilder<RealmResultsChanges<MainObj>>(
stream: realmServices.realm.all<MainObj>().changes,
builder: (context, snapshot) {
final data = snapshot.data;
if (data == null) return waitingIndicator();
final results = data.results;
return ListView.builder(
shrinkWrap: true,
itemCount: results.realm.isClosed ? 0 : results.length,
itemBuilder: (context, index) => results[index].isValid
? Container(
margin: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 4.0,
),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(12.0),
shape: BoxShape.rectangle,
),
child: MainObjItem(results[index]))
: Container());
},
),
),
),
],
),
realmServices.isWaiting ? waitingIndicator() : Container(),
],
);
}
and the ListView.builder invokes a Stateless Widget build (seems to be ok as well)…
class MainObjItem extends StatelessWidget {
final MainObj _mainObj;
const MainObjItem(this. _mainObj, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(_mainObj.name),
subtitle: Text(
_getAnotherCollectionName(context, _mainObj.embedded[0].anotherCollectionId)), // this checks out fine, there is always a property here
onTap: () => {
infoMessageSnackBar(context, "Not yet implemented").show(context),
},
);
}
String _getAnotherCollectionName(BuildContext context, ObjectId id) =>
Provider.of<AnotherCollectionModel>(context, listen: false). getAnotherCollectionName(id);
}
and finally, a ChangeNotifier that queries the realm by id to find the document in the other collection…
class AnotherCollectionModel extends ChangeNotifier {
Realm _realm;
AnotherCollectionModel(this._realm);
String getAnotherCollectionName(ObjectId id) {
var queryStr = '_id == oid(${id.toString()})';
var anotherCollection = _realm.query<AnotherCollection>(queryStr);
return anotherCollection.length > 0 ? anotherCollection.name : "NOT FOUND";
}
}
This, finally, is where my query is failing. I’ve confirmed the id coming in is correct, exists in the collection I’m querying, but both realm.query() and realm.find() don’t return any results for me.
I actually tested this last AnotherCollectionModel successfully using an id from an object that isn’t from an embedded object, it fails when the id is from an embedded object (To be precise, an id contained in an embedded object in a list held by the main object).
I’ve tried realm.query() with id as a parameterized query, ie
`realm.query( '_id == $0, [id])';`
along with the way I show above, and using realm.find(). None of them seem to work.
I’m not understanding why this fails, or a good way to troubleshoot it.
Thanks