0

I want to first check all the drivers nearby by selecting type of driver. and then get their distance from the client and than find the nearest driver among type and send the request, but my code is only getting the first driver in the list and sends the request thereafter.

void searchNearestDriver()
  {
    if(availableDrivers.length == 0)
    {
      cancelRideRequest();
      resetApp();
      noDriverFound();
      return;
    }

    var driver = availableDrivers[0];

    driversRef.child(driver.key).child("type").once().then((DataSnapshot snap) async
    {
      if(await snap.value != null)
      {
        String carType = snap.value.toString();
        print("Driver from db " + carType);
        if(carType == carRideType)
        {
          notifyDriver(driver);
          availableDrivers.removeAt(0);
        }
        else
        {
          print(carRideType + " Cannot be found");
          displayToastMessage(carRideType + " not available. Try again.", context);
        }
      }
      else
      {
        displayToastMessage("No driver found. Try again.", context);
      }
    });

  }

  • You'll want to have a look at GeoFire or a similar library for Flutter, which allows such so-called geoqueries. See https://stackoverflow.com/questions/43357990/query-for-nearby-locations/43358909#43358909. A quick [search for Flutter version of the library](https://www.google.com/search?q=geofire+for+flutter) gives promising results. – Frank van Puffelen Nov 07 '21 at 20:46

0 Answers0