Welcome back.
We talked about flow and how important it is for fetching real time updates.
We said that flow is a called asynchronous stream that emits values over time.
We need to put real time data in this flow in order to return this function.
A flow of list of destination markers.
So from where we need to get the real time updates, this code gets the data once when the app launches.
And this is a problem if there is a new marker inserted into database during the app run.
This new marker will not be represented on the map.
And this is what we've seen in the previous videos.
Here we need to listen for real time updates from Firestore collection, which is the marker for this.
Remove this code.
This code the previous code.
Get the data once during the runtime.
We don't need to get only one time.
We need to fetch and get real time data changes.
For this use val listener equals to firebase dot collection.
Set the collection the same as before.
And here the newly used code is add snapshot listener.
Here we are listening for real time updates from Firestore collection named markers.
Whenever the data in markers changes added, modified or removed, the snapshot callback is triggered.
If an error occurs, new network issues permission denied.
The error callback is executed.
Okay, so this is the idea behind getting the real time updates.
Okay, so whenever data changes, snapshot is triggered.
Whenever an error occurs, the error callback is executed.
As we said before, the collection of Firebase Firestore is named markers that add the snapshot listener
attaches a real time listener to the collection.
So here we are attaching a real time listener for the collection.
I want from you to focus with me and write the notes down.
Because these notes are very important and help you in memorizing every code.
Okay, here we are attaching a real time listener to the collection.
The snapshot a query snapshot containing the latest data.
Error an exception if something went wrong.
Let me continue with this code here.
Let me start with if start with if error is not null.
So if there is an error what we need to do return add snapshot listener.
And this is error dot print stack trace and return and snapshot listener.
It exits the listener callback early to avoid processing invalid data.
This is in case of an error.
Now let's handle if there is no error.
Now use.
If snapshot does not equal to null and snapshot is not empty.
So there is no null values and it's not empty.
Let me use vowel markers equals to snapshot dot map.
We used map in the previous videos, but I'll show you here.
Fetched marker or my marker.
You can use my marker.
And here let me customize it as a destination marker okay.
Here like this.
And let me explain what we've done here.
Here we are checking if snapshot is valid.
It's not null ensures that query snapshot Firebase response is not null and snapshot is not empty.
Checks if the snapshot contains documents.
Avoids processing empty results if both conditions pass, the snapshot has valid data to process and
convert Firestore documents to destination marker objects.
Snapshot dot map iterates iterates over over each document query document snapshot in the snapshot and
maps it to a destination marker.
So every document, every marker in the markers list would be customized into a destination marker object.
The fields marker ID marker that got double latitude longitude markers dot get double longitude and
marker dot get string that title.
So here we are getting every document in the snapshot.
Go and create a destination marker and fetch the IDs, the latitude, longitude and title variables
from this document.
Now we need to emit map data via try sent here outside the if block.
Use dry sand markers, sending the mapped list destination marker to the flow consumers, for example,
Jetpack Compose UI and part of the callback flow or mutable shared flow API, which is used to bridge
callback based APIs like Firebase Firestore.
Listeners with Kotlin Flow and we can handle empty or invalid snapshots using try or else try send an
empty list, then remove this return and here use await close and set the listener remove in the preview.
In the next videos, we're going to learn about the callback and how to solve those errors and how to
import the correct package.