FreakShow
FreakShow

Reputation: 87

Kotlin Multiplatform and Firestore Integration

I am attempting to build a Kotlin multiplatform app, after getting severely frustrated at the React Native incomprehensible error messages.

I've been able to follow the Kotlin guides and been very comfortable with how this has gone. But now, I'm trying to fetch the data I have in firestore, just retuning a basic item.

EDIT: I have since moved on and found that dev.gitlive does a firestore kotlin first SDK: https://github.com/GitLiveApp/firebase-kotlin-sdk

But I am not able to understand or find a guide on how to actually use this library....

My issue is that I have added firestore as a dependency, but I can't seem to import firebase correctly to actually use it.

My build.gradle.kts contains:

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
            implementation("io.ktor:ktor-client-core:$ktorVersion")
            implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
            implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
            implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
            implementation("com.google.firebase:firebase-bom:28.4.1")
            implementation("com.google.firebase:firebase-firestore-ktx:21.4.0")
        }
    }

And when importing it into my commonMain code I can do:

import com.google.firebase.*

import com.google.firebase.ktx.*

But I can't do: import com.google.firebase.Firebase

import com.google.firebase.ktx.Firebase

As the firebase part will be highlighted in red as if it's an incorrect import. Even taking that off and just doing the import all command, no firebase functionality/class is found.

Following the google docs for Kotlin+KTX:

dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:31.2.3')

// Declare the dependency for the Cloud Firestore library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-firestore-ktx'
}

Doesn't work either, as it complains that:

Could not find com.google.firebase:firebase-firestore-ktx:.
     Required by:
         project :shared

Any help/guidance would be really appreciated.

Upvotes: 3

Views: 2041

Answers (1)

Archie G. Quiñones
Archie G. Quiñones

Reputation: 13668

Firebase, in itself, does not support kotlin multiplatform yet. Thats why adding it to commonMain dependencies doesn't work. If you want to use Firebase for both platforms, you'll have to do it yourself, by stitching expect/actual to your target platforms. Gitlive Firebase does this for you thats the reason why Gitlive works in your project.

Upvotes: 1

Related Questions