I am working in a flutter where I am expecting the UI to be updated with data when backend insert any in mongoDB collection. For futter I am using realm.
Currently I am trying to listen changes in mongoDB but it is not triggering when data getting inserted in to the mongoDB
Pasting my code here.
class UserPlantRepo extends UserPlantRepoInterface {
final RealmServiceInterface _realmService;
final localDataStream = StreamController<RealmResultsChanges<UserPlant>?>.broadcast();
UserPlantRepo({
required RealmServiceInterface realmService,
}) : _realmService = realmService
{
localDataStream.stream.listen((event) {
log("UserPlantRepo localDataStream event emitted $event");
},onError: (error){
log("UserPlantRepo localDataStream error emitted $error");
},);
}
@override
Future<UserPlantDataModelWrapper> getUserPlantsFromDB(
{required String emailAddress}) async {
try {
await _realmService.updateSubscriptions<UserPlant>(
r'userId==$0',
[emailAddress],
);
final RealmResults<UserPlant>? response = await _realmService.fetchData(
r'userId==$0',
[
emailAddress,
],
);
if(response != null){
log("UserPlantRepo getUserPlantsFromDB response is not null");
localDataStream.addStream(response.changes);
}
;
if (response != null) {
if (response.isNotEmpty) {
return UserPlantDataModelWrapper(
isSuccess: true,
userPlantDataModel: response.first.getUserPlants());
} else {
return UserPlantDataModelWrapper(
isSuccess: true, userPlantDataModel: null);
}
} else {
return UserPlantDataModelWrapper(
isSuccess: true, userPlantDataModel: null);
}
} catch (e) {
return UserPlantDataModelWrapper(
isSuccess: true, userPlantDataModel: null);
}
}
Stream<RealmResultsChanges<UserPlant>?> get getUpdateStatusStream => localDataStream.stream;
}
Please help on this.
Thanks in advance!!