Skip to content
Advertisement

I am getting error in firebase database function get()

I am facing a problem for 2-3 days I am trying to fix it but I can’t fix it. I am continuously getting errors on a Firebase Realtime Database function get() on a code that I have copied from Firebase Realtime Database documentation. Here is my code:

private DatabaseReference mDatabase;

// ...
mDatabase = FirebaseDatabase.getInstance().getReference("user app/profile");
//...
mDatabase.child(uid).get().addOnCompleteListener(new OnCompleteListener < DataSnapshot > () {
    @Override
    public void onComplete(@NonNull Task < DataSnapshot > task) {
        if (!task.isSuccessful()) {
            _custom_smart_toast(String.valueOf(task.getException()), setting.getString("color", ""), 10, setting.getString("color", ""));
        } else {
            _custom_smart_toast(String.valueOf(task.getResult().getValue()), setting.getString("color", ""), 10, setting.getString("color", ""));
        }
    }
}

Build.gradle:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 28

    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.tsd.html"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.3"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'com.google.firebase:firebase-auth:19.0.0'
    implementation 'com.google.firebase:firebase-database:19.0.0'
    implementation 'com.google.firebase:firebase-storage:19.0.0'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation 'com.google.code.gson:gson:2.8.7'
    implementation 'com.squareup.okhttp3:okhttp:3.9.1'
}

I am getting error on compiling code that is:

The method get() is undefined for the type DatabaseReference

Advertisement

Answer

The get() method isn’t recognized because you’re using an old version of the Realtime Database dependency. The get() method is available starting from version 19.6.0.

To be able to use get(), you need to update the dependency to the latest available version, which is:

implementation 'com.google.firebase:firebase-database:20.1.0'

But I strongly advise you to use the BOM version without specifying the version for each product that you’re using:

implementation platform("com.google.firebase:firebase-bom:31.0.2")
implementation "com.google.firebase:firebase-database"
implementation "com.google.firebase:firebase-auth"
implementation "com.google.firebase:firebase-storage"

If you’re using Kotlin, here are the corresponding dependencies:

implementation platform("com.google.firebase:firebase-bom:31.0.2")
implementation "com.google.firebase:firebase-database-ktx"
implementation "com.google.firebase:firebase-auth-ktx"
implementation "com.google.firebase:firebase-storage-ktx"
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement