avatarNiranjanky

Summary

The article discusses integrating Firebase with Compose Multiplatform using the Kotlin-first SDK for Firebase, detailing the steps for setting up a multiplatform app with Firebase support for Android and iOS.

Abstract

The author shares their findings on using Firebase with Compose Multiplatform, emphasizing the use of the Kotlin-first SDK for Firebase. This SDK allows for multiplatform support, enabling developers to target iOS, Android, Desktop, or Web from a common source. The article outlines a seven-step process: creating a Compose Multiplatform app using a template-driven approach, setting up Firebase projects for Android and iOS, configuring these projects, adding data to Firestore, incorporating GitLive dependencies, and finally, running the app on both Android and iOS platforms to display data from Cloud Firestore. The author provides detailed instructions and code snippets, including the use of Kotlin extensions for Firebase Android SDK and the GitLive Firebase SDK for Kotlin.

Opinions

  • The author suggests avoiding the Kotlin Multiplatform Wizard for iOS Firebase setup due to potential issues, favoring a template-driven approach instead.
  • The article implies that using the Firebase Kotlin SDK is beneficial for Compose Multiplatform projects due to its Kotlin-first API and multiplatform support.
  • The author's guidance to "check out the blog for details" at various steps suggests that there is additional valuable information available in related blog posts, indicating a comprehensive resource for developers.
  • By demonstrating the successful loading of data from Cloud Firestore into the app, the author positively endorses the effectiveness of the outlined integration process.

I tried integrating Firebase in the Compose Multiplatform, check out what I found out.

I was looking for a Multiplatform libary for firebase and i found out this.

Multiplatform Library for Firebase:

The Firebase Kotlin SDK is a Kotlin-first SDK for Firebase. It’s API is similar to the Firebase Android SDK Kotlin Extensions but also supports multiplatform projects, enabling you to use Firebase directly from your common source targeting iOS, Android, Desktop or Web, enabling the use of Firebase as a backend for Compose Multiplatform, for example.

Let’s start with the implementation:

1. Create your Compose Multiplatform App — check out the blog for details

Note* Don’t use the Kotlin Multiplatform Wizard because it is creating a problem for the iOS Firebase setup so the correct way is to use the template driven approach for creating your Compose Multiplatform App

2. Download and launch the app in Android Studio.

3. Create a firebase project — check out the blog for details

4. Configure your Firebase project for Android App — check out the blog for details

5. Configure your Firebase project for iOS App — check out the blog for details

6. Add data to the Firestore to later show in the UI — check out the blog for details

7. Add the GitLive dependencies

Go to build.gradle.kts(:composeApp)

Create a User class:

Get the collection from Firebase Firestore

Update the App composable function as follows:

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.firestore.firestore
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource

@OptIn(ExperimentalResourceApi::class)
@Composable
fun App() {
    MaterialTheme {
        var greetingText by remember { mutableStateOf("Firebase Compose Multiplatform!") }
        var showImage by remember { mutableStateOf(false) }
        Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
            var list by remember { mutableStateOf(listOf<User>()) }
            LaunchedEffect(Unit){
                list = getUsers()
            }
            LazyColumn {
                items(list) {
                    UserItem(it)
                }
            }
            Button(onClick = {
                greetingText = "Compose: ${getPlatformName()}"
                showImage = !showImage
            }) {
                Text(greetingText)
            }
            AnimatedVisibility(showImage) {
                Image(
                    painterResource("compose-multiplatform.xml"),
                    null
                )
            }
        }
    }
}

expect fun getPlatformName(): String
@Composable
fun UserItem(user: User){
    Column {
        Text(text = user.location)
        Text(text = user.day)
    }
}
suspend fun getUsers(): List<User> {
    val firebaseFirestore = Firebase.firestore
    try{
        val userResponse = firebaseFirestore.collection("Users").get()
        return userResponse.documents.map {
            it.data()
        }
    } catch (e: Exception){
        throw e
    }
}

Run your androidApp

Run also for iosApp

It loaded the data from the Cloud Firestore

Thanks;)

Compose Multiplatform
Android
App Development
iOS
Kotlin
Recommended from ReadMedium