So I have been working with firestore recently but I have had been having a hard time retrieving object from my database. I want to have a method that retrieves an object and returns it, I tried to look up up this question but it seems that the only solution is to use a class attribute however the problem is that it is a static method. Here is the code:
package com.example.test2;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
public class DatabaseManager {
private static final FirebaseFirestore db = FirebaseFirestore.getInstance();
private static final String TAG = "error";
private static User retrievedUser;
public static void addUser(User user){
// Add a new document with a generated ID
db.collection("brawlrUsers").document(user.getId())
.set(user);
}
public static void setAttribute(String attribute, String value){
// db.collection("brawlrUsers")
}
//----------------------------------------
// this is the method I need help with, I want to return the user
// from inside the successlistener but I can't since it has void return type
//----------------------------------------
public static User getUser(String username){
db.collection("brawlrUsers").document(username).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
User user = documentSnapshot.toObject(User.class);
}
});
return new User();
}
}