0
0
Android Kotlinmobile~7 mins

Cloud Firestore integration in Android Kotlin

Choose your learning style9 modes available
Introduction

Cloud Firestore lets your app save and get data from the cloud easily. It helps your app share data between users and devices in real time.

You want to save user notes or messages online so they can access them from any device.
Your app needs to show live updates, like chat messages or scores, without refreshing.
You want to store app settings or user profiles in a central place.
You want to build a to-do list app that syncs tasks across phones.
You want to keep track of user activity or preferences in the cloud.
Syntax
Android Kotlin
val db = FirebaseFirestore.getInstance()
db.collection("collectionName").document("docId").set(dataMap)
  .addOnSuccessListener { /* success code */ }
  .addOnFailureListener { /* failure code */ }

Use getInstance() to get the Firestore database object.

Use collection() and document() to point to where you want to save or read data.

Examples
This saves a user with name and age to the 'users' collection under 'user1' document.
Android Kotlin
val db = FirebaseFirestore.getInstance()

// Save data to a document
val user = hashMapOf("name" to "Alice", "age" to 25)
db.collection("users").document("user1").set(user)
This reads the 'name' field from the 'user1' document in 'users' collection.
Android Kotlin
val db = FirebaseFirestore.getInstance()

// Read data from a document
val docRef = db.collection("users").document("user1")
docRef.get().addOnSuccessListener { document ->
  if (document != null && document.exists()) {
    val name = document.getString("name")
  }
}
This listens for live changes to the 'user1' document and updates the app automatically.
Android Kotlin
val db = FirebaseFirestore.getInstance()

// Listen for real-time updates
val docRef = db.collection("users").document("user1")
docRef.addSnapshotListener { snapshot, e ->
  if (e != null) return@addSnapshotListener
  if (snapshot != null && snapshot.exists()) {
    val age = snapshot.getLong("age")
  }
}
Sample App

This app saves a user named Bob with age 30 to Firestore. It shows a message on screen if saving worked or failed.

Android Kotlin
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.firestore.FirebaseFirestore

class MainActivity : AppCompatActivity() {
  private lateinit var db: FirebaseFirestore
  private lateinit var textView: TextView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    textView = TextView(this)
    setContentView(textView)

    db = FirebaseFirestore.getInstance()

    val user = hashMapOf("name" to "Bob", "age" to 30)
    db.collection("users").document("user1").set(user)
      .addOnSuccessListener {
        textView.text = "User saved successfully"
      }
      .addOnFailureListener {
        textView.text = "Failed to save user"
      }
  }
}
OutputSuccess
Important Notes

Make sure to add Firebase to your Android project and include Firestore dependencies.

Firestore operations are asynchronous, so use listeners to handle success or failure.

Always check for null or errors when reading data to avoid crashes.

Summary

Cloud Firestore stores and syncs app data in the cloud.

Use FirebaseFirestore.getInstance() to start using Firestore.

Save, read, and listen to data with simple Kotlin code and listeners.