I am working on a Flutter project which is a tricycle booking system that also uses Realtime Database in Firebase. If the ride status of the passenger has been accepted by the driver, an audio or notify sound will be executed then a modal popup/dialog will be displayed.
Upon trying, these two functionalities have been executed indefinitely and multiple times as long as the ride status of the passenger is accepted. How do I implement it once only and what is the reason behind it because I am not using any loop for this?
Here are the updated code for this method:
saveRideRequestInformation() //Ride Request Code { //1. save the Ride Request Information referenceRideRequest = FirebaseDatabase.instance.ref().child("All Ride Requests").push(); // Creates unique ID String? rideKey = referenceRideRequest!.key.toString(); var originLocation = Provider.of<AppInfo>(context, listen: false).userPickUpLocation; var destinationLocation = Provider.of<AppInfo>(context, listen: false).userDropOffLocation; Map originLocationMap = { //key:value "latitude": originLocation!.locationLatitude.toString(), "longitude": originLocation!.locationLongitude.toString(), }; Map destinationLocationMap = { //key:value "latitude": destinationLocation!.locationLatitude.toString(), "longitude": destinationLocation!.locationLongitude.toString(), }; Map userInformationMap = { "origin": originLocationMap, "destination": destinationLocationMap, "time": DateTime.now().toString(), "username": userModelCurrentInfo!.username!, "email": userModelCurrentInfo!.email!, "id": userModelCurrentInfo!.id!, "requestId": rideKey, "originAddress": originLocation.locationName, "destinationAddress": destinationLocation.locationName, "driverId": "waiting", "notified" : "false", }; referenceRideRequest!.set(userInformationMap); tripRideRequestInfoStreamSubscription = referenceRideRequest!.onValue.listen((eventSnap) async // getting updates in real time { if(eventSnap.snapshot.value == null) { return; } if ((eventSnap.snapshot.value as Map)["driverPlateNum"] != null) //!! GAWING CAR DETAILS/ PLATE NUMBER { setState(() { driverTricDetails = (eventSnap.snapshot.value as Map)["driverPlateNum"].toString(); }); } if ((eventSnap.snapshot.value as Map)["driverPhone"] != null) //!! GET PHONE NUMBER { setState(() { driverPhone = (eventSnap.snapshot.value as Map)["driverPhone"].toString(); }); } if ((eventSnap.snapshot.value as Map)["notified"] != null) //!! GET PHONE NUMBER { setState(() { notified = (eventSnap.snapshot.value as Map)["notified"].toString(); }); } if ((eventSnap.snapshot.value as Map)["driverName"] != null) //!! GET FNAME { setState(() { driverName = (eventSnap.snapshot.value as Map)["driverName"].toString(); }); } if((eventSnap.snapshot.value as Map)["status"] != null) { setState(() { userRideRequestStatus = (eventSnap.snapshot.value as Map)["status"].toString(); }); } if((eventSnap.snapshot.value as Map)["driverLocation"] != null) { double driverCurrentPositionLat = double.parse((eventSnap.snapshot.value as Map)["driverLocation"]["latitude"].toString()); double driverCurrentPositionLng = double.parse((eventSnap.snapshot.value as Map)["driverLocation"]["longitude"].toString()); LatLng driverCurrentPositionLatLng = LatLng(driverCurrentPositionLat, driverCurrentPositionLng); if(userRideRequestStatus != null) { isVisible= !isVisible; showUIForAssignedDriverInfo(); //when status = accepted if(userRideRequestStatus == "accepted" && notified == "false") { FirebaseDatabase.instance.ref() .child("All Ride Requests") .child(rideKey) .child("notified") .set("true"); passengerIsOfflineNow(); assignedDriverModal(); updateArrivalTimeToUserPickupLocation(driverCurrentPositionLatLng); } //when status = arrived if(userRideRequestStatus == "arrived") { setState(() { driverRideStatus = "Your driver has arrived."; }); } //when status = onTrip if(userRideRequestStatus == "onTrip") { updateReachingTimeToUserDropOffLocation(driverCurrentPositionLatLng); } //when status = ended if(userRideRequestStatus == "ended") { if((eventSnap.snapshot.value as Map)["fareAmount"] != null) { double fareAmount = double.parse((eventSnap.snapshot.value as Map)["fareAmount"].toString()); var response = await showDialog( context: context, barrierDismissible: false, builder: (BuildContext c) => PayFareAmountDialog( fareAmount: fareAmount, ), ); if(response == "cashPayed") { //user can rate the driver if((eventSnap.snapshot.value as Map)["driverId"] != null) { String assignedDriverId = (eventSnap.snapshot.value as Map)["driverId"].toString(); Navigator.push(context, MaterialPageRoute(builder: (c)=> RateDriverScreen( assignedDriverId: assignedDriverId, ))); referenceRideRequest!.onDisconnect(); tripRideRequestInfoStreamSubscription!.cancel(); } } } } } } }); onlineNearbyAvailableDriversList = GeoFireAssistant.activeNearbyAvailableDriversList; //searchNearestOnlineDrivers(); }