How I can send request to the nearest app user just like Uber in flutter

Issue

I want to create an application just like Uber using Firebase. I do not under stand how I can show the nearest application user on google map using costume markers and how I can send the request to the all nearest application users at same time just like Uber do.

Solution

Since you’re already using Firebase, you can use Firebase Cloud Messaging. This is obviously a rather big feature that will envolve lots of sub-tasks, but here’s a simplified overview of how it could be:

You have three players involved:

  1. the "Uber user" client app
  2. the "Uber driver" client app
  3. your app server (which could be Firebase Cloud Functions, for a serverless backend)

The data flow could be as follows:

  1. "Drivers" open their apps. This establishes a connection with the server, and keeps the server constantly updated with their geolocation.

  2. A "user" opens his app. This triggers a request to the server sending the user’s geolocation.

  3. The server (which knows the realtime location of all drivers) calculates which drivers are near the user, and responds the user request with this data. The user client app can now render a map widget with the drivers locations (for this, you’ll probably use a package like google_maps_flutter).

  4. The user clicks a button to request a ride. This again triggers a request to the server.

  5. The server receives the request, and notifies the nearby drivers using Cloud Messaging. FCM has a message type called Data message which is well suited for this; you can send custom data, and the client app will process it however it wants.

  6. The drivers’ apps receives this Data Message and render the UI showing there’s a ride available. If the driver accepts the ride, this sends a request to the server.

  7. Once the server sees the "accepted ride" request, it sends another message to all other drivers informing the ride is no longer available, as well as sends a message to the user informing the ride has been accepted.

As I said, this is not a simple feature. There are several tricky parts, such as race conditions, and making sure only a single driver accepts a ride. But this should be a high-level overview of how it can be done.

Finally, this schema is a quite technology agnostic; it isn’t specific to Flutter. The architecture to have that feature could be implemented like this in pretty much any modern mobile framework – Flutter is just a UI framework.

Answered By – Luis Fernando Trivelatto

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.