Congratulations guys.
We succeeded in getting all markers from Firebase Firestore.
Now I'm going to show you another way to get and store markers in compose.
As I told you this is a for compose and compose UI.
So you can create a new ordinary XML file.
Create a new um like Kotlin or Java code and use those variables with them.
But since we are working with compose, I will show you the new type and the new version and new approach
of this code.
So comment this.
I'll keep it if you if you want another way and keep it for later.
Differences.
Val markers equals to or it's of type snapshot state list destination marker mutable state list of.
Let me explain what we've done here.
This line Creates an observable, mutable list of destination marker items that Jetpack Compose can
track for recomposition.
This line creates an observable, mutable list of destination marker items that Jetpack Compose can
track for recomposition.
Mutable state list of creates a mutable list that is observable by compose.
Snapshot aware compose keeps track of its state.
You can use dot add items, dot remove items, and dot clear function whenever you change the list.
Compose Recomposes anything that use it.
Compose will automatically update the UI for any change and the snapshot state list.
This is the type of list.
Snapshot state list is basically a special compose collection that notifies recomposition when its content
change.
Think of it as a mutable list plus composite state awareness.
Mutable list.
We use it in and LiveData in the previous examples and in previous course with XML and Java.
Now we are using mutable list plus composite state awareness, which is inside the state snapshot state
list type.
Any composable that reads markers will recompose when you add items, remove items, or update items.
Please, please please guys, write those notes down because those are very important things and you
should remember them when you work later in your future projects so you don't be confused when working
with complex application and complex projects.
I want from you to focus with me and learn the difference and how I think, and how you develop your
code without any errors.
And what things?
What codes are specific and more convenient for your projects?
Keep the init block the same.
Load markers from Firestore.
And here we need to modify.
So instead of using this markers dot value we can directly use val new markers equals to repository
marker repository.
Get all from Firestore.
Markers dot clear and then markers dot add all new markers.
Here I'm clearing the list then adding the newly created markers and newly created and updated markers.
Okay then here markers dot clear.
Now let's move to the Uber clue map marker val markers by.
Remember I need to remove it.
And here equals to remember ViewModel dot markers alt plus enter to import it.
This prevents recreating the state reference on recomposition.
The snapshot state list is threadsafe for UI updates.
All modifications should happen in Viewmodels coroutine scope.
The pros are simpler code, direct access, no flow operations.
The cons are ties viewmodels to compose runtime less flexible for non compose use.
As I told you, this is the uh the the way or approach for compose only UI.
So let me mark it like this for compose only twice and this for compose and compose UI approach.
Let's run our application.
Here we go.
This is our map and this is our marker.
Now let's create another document.
The ID would be for example Paris.
The first field is latitude.
It's of type number the value for 8.8575.
And let's add a new field.
Longitude number 2.3514.
It's east, so it's positive.
Let me see if there is an instant initialization and inserting into the database.
Click save and new marker being added to our database.
So this is a big problem.
We added the the marker but it um doesn't appear on the map.
If we run the application again two markers are being displayed in France and in San Francisco.
So we need to listen for the live updates and handle the live updates of the markers in the next video.
We're gonna solve this issue.