Using rxdart to Debounce Geolocator Updates for Real-Time Driver Location in Flutter App updates drivers data only once

48 views Asked by At

I'm developing an app with car drivers. When the driver is marked as active, it is shown as active in Firebase, and their position is constantly updated. However, I realized that it's not necessary to update the driver's position every second; updating every five or seven seconds would be sufficient. So, I looked for a way to delay the events generated by getPositionStream() from Geolocator by 5 seconds between each call. I found that I could use a package called "rxdart," so I implemented it. However, now the driver's location isn't updating at all.

streamSubscriptionPosition = debouncedStream.listen((Position position) {
    driverCurrentPosition = position;

    if (isDriverActive == true) {
      Fluttertoast.showToast(msg: "ssss");
      Geofire.setLocation(
        currentFirebaseUser!.uid,
        driverCurrentPosition!.latitude,
        driverCurrentPosition!.longitude,
      );

      if (driverIsInRange(
          driverCurrentPosition!.latitude, driverCurrentPosition!.longitude)) {
        driverIsInQueueArea = true;
        addDriverToQueue();
      } else if (driverIsInQueueArea == true) {
        AssistantMethods.removeDriverFromQueue(
          context,
          "You're out of range, so you've been removed from the driver queue. You will now receive trips based on geographic proximity.",
        );
        driverIsInQueueArea = false;
      }
    }

    LatLng latLng = LatLng(
      driverCurrentPosition!.latitude,
      driverCurrentPosition!.longitude,
    );

    newGoogleMapController!.animateCamera(CameraUpdate.newLatLng(latLng));
  });
}

I tried using debouncedStream, but it's not working as expecte, but before I tried to use the method "throttleDuration" but it didn't get recognized as a function, I'm correctly importing the package rxdart

0

There are 0 answers