Exploring Firebase Firestore in Android with Jetpack Compose UI

Abhishek Pathak
4 min readAug 26, 2023

--

In the modern landscape of Android app development, creating efficient and dynamic databases is essential to provide users with seamless experiences. Firebase Firestore, a cloud-hosted NoSQL database, has gained immense popularity among developers due to its real-time capabilities, scalability, and ease of integration. Combining Firestore with Jetpack Compose UI, a modern Android UI toolkit, opens up new avenues for building responsive and visually appealing database-driven applications. In this article, we will delve into the process of setting up a Firestore database in an Android app using Kotlin and Jetpack Compose UI.

Firebase Firestore?

Firebase Firestore is a cloud-based NoSQL database that enables real-time synchronization of data across multiple devices and platforms. It organizes data in collections and documents, making it easy to manage and retrieve information. Firestore’s real-time capabilities allow applications to instantly reflect changes made to the database, making it ideal for collaborative and data-intensive applications.

Setting Up Firebase Firestore

Before integrating Firebase Firestore into your Android app, you need to set up a Firebase project and add the necessary dependencies to your app’s build.gradle file. Here’s a step-by-step guide:

  1. Create a Firebase Project:
  • Go to the Firebase Console (console.firebase.google.com).
  • Click “Add Project” and follow the setup instructions.
  • Once your project is created, click “Continue” to proceed to the project dashboard.

2. Add Firebase to your Android App:

  • Click the “Android” icon to add your app to the project.
  • Follow the instructions to provide your app’s package name and other details.
  • Download the google-services.json file and place it in the app module's root directory.

3. Open Firebase Assistant:

  • In Android Studio, go to “View” > “Tool Windows” > “Firebase” to open the Firebase Assistant tool window.

4. Connect Project to Firebase:

  • If you’ve already connected your project to Firebase, you can skip this step. Otherwise, click on the “Connect to Firebase” button in the Firebase Assistant window and follow the prompts to connect your project.

5. Add Firestore Dependencies:

  • In the Firebase Assistant window, navigate to “Develop” > “Firestore” and click on “Learn more.”
  • Click on the “Add Firestore to your app” button.
  • This will open a dialog showing the necessary Gradle dependencies to add to your project.
implementation 'com.google.firebase:firebase-firestore-ktx:23.0.0'

6. Sync Gradle Files:

  • After adding the dependency, click on the “Sync Now” button that appears in the Android Studio toolbar.

7. Initialize Firestore:

  • In your app’s Application class or any suitable entry point, initialize Firestore using the FirebaseFirestore instance:
FirebaseApp.initializeApp(this)
val firestore = FirebaseFirestore.getInstance()

Creating a Firestore Database with Jetpack Compose UI

Now that you have Firebase Firestore set up, let’s integrate it with Jetpack Compose UI to create a basic database-driven UI.

Define Data Model:

Create a data class to represent the data you want to store in the Firestore database.

data class Note(val id: String = "",
val title: String = "",
val content: String = ""
)

Read Data from Firestore:

  • Use Firestore’s collection() and get() methods to fetch data from the database.
  • Convert the retrieved data to your data model.
val notesCollection = firestore.collection("notes")
notesCollection.get()
.addOnSuccessListener { result ->
for (document in result) {
val note = document.toObject(Note::class.java)
// Process the note data
}
}
.addOnFailureListener { exception ->
// Handle error
}

Display Data using Jetpack Compose:

Create a Composable function to display the data using Jetpack Compose UI components.

@Composable
fun NoteList(notes: List<Note>) {
LazyColumn {
items(notes) { note ->
Text(text = note.title)
Text(text = note.content)
}
}
}

Combine Firestore and Jetpack Compose:

Integrate the Firestore data retrieval with the Composable function

val notes = remember { mutableStateListOf<Note>() }

SideEffect {
notesCollection.get()
.addOnSuccessListener { result ->
for (document in result) {
val note = document.toObject(Note::class.java)
notes.add(note)
}
}
.addOnFailureListener { exception ->
// Handle error
}
}

NoteList(notes = notes)

Conclusion

Combining Firebase Firestore and Jetpack Compose UI offers a powerful solution for creating dynamic and engaging Android applications. By seamlessly integrating Firebase’s real-time database capabilities with Jetpack Compose’s declarative UI approach, developers can build responsive and data-driven apps that provide exceptional user experiences. Whether you’re building collaborative note-taking apps, messaging platforms, or any other real-time application, the combination of Firebase Firestore and Jetpack Compose UI opens up a world of possibilities for your Android development journey.

Thank you for reading my article. I really appreciate your response.

Clap if this article helps you. If I got something wrong, please comment for improve.
let’s connect on
Linkedin , GitHub

--

--