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:
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<String, String> bilgiler = new HashMap<>();
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())) {
//Log.d("TAG", document.getId() + " => " + document.getData());
bilgiler.put("asdf", document.getData().toString());
}
} else {
Log.w("TAG", "Error getting documents.", task.getException());
}
}
});
Log.i("asdfads",(bilgiler.get("asdf")));