Skip to content Skip to sidebar Skip to footer

How Can I Solve This Android Studio Firebase Problem?

I am trying to get data from Firebase to bilgiler variable. But I get error all the time. How can I solve this? Map bilgiler = new HashMap<>(); db.coll

Solution 1:

Data is loaded from Firebase asynchronously. Right now your Log.i("asdfads",(bilgiler.get("asdf"))); is run before bilgiler.put("asdf", document.getData().toString()) runs, which explains why you don't see the value from the database.

Any code that needs data from the database, needs to be inside the onComplete method, or be called from there. So:

db.collection("tarihBilgi")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                    bilgiler.put("asdf", document.getData().toString());
                    Log.i("asdfads",(bilgiler.get("asdf")));
                }
            } else {
                Log.w("TAG", "Error getting documents.", task.getException());
            }
        }
    });

Also see:

Post a Comment for "How Can I Solve This Android Studio Firebase Problem?"