I have a flutter application that is using Cloud Firestore. My application needs to listen for changes in a document but does not need to change any UI elements hence I am using .listen() (Please correct me if I should be using stream builder instead). I have noticed that per change in data, the below print statement runs twice. Is there something that I am doing wrong here?
static Future<void> checkForRequests() async {
print('Request call initiated');
try {
_firestore
.collection('Requests')
.where('UsersList', arrayContains: _auth.currentUser.uid)
.snapshots()
.listen((event) {
event.docs.forEach((element) {
print('Checking for requests');
final person = element.get('ActionRequiredBy');
if (person == _auth.currentUser.uid) {
final stage = element.get('Stage');
final requestID = element.get('ConversationID');
switch (stage) {
case 3:
{
JPAKEStages.stage3(requestID);
}
break;
case 4:
{
JPAKEStages.stage4(requestID);
}
break;
case 5:
{
JPAKEStages.stage5(requestID);
}
break;
case -1:{
}
break;
default:
{
return;
}
}
}
return;
});
});
} catch (Exception) {
print(Exception);
}
}
There is just a single document but the print statement 'Checking requests' gets called twice every time there is a change in that document.