Problem 1
- How to convert Task to object
We can do this in Kotlin very easily, but now in the case of Java it seems to be hard and I wanted to know how we can do this in java.
public void addUserDetails(){
// Already done: no problem in this section
}
public Task<DocumentSnapshot> getUserDetails(String user_id){
return collectionReference.document(mAuth.getUid()).get();
}
This is in my userDAO.java or user data access object to get data.
I want this data to my PostSection.java where the user's name or other data will be visible.
where I've seen that we can easily convert Task to Object by using this line in Kotlin
// Here User is model class
val User = userDao.getUserDetails(user_id).await().toObject(User::class.java)
similarly, I want to do this in Java.
Problem 2
- So, Basically, I've to retrieve data from the Firestore and used it outside of OnSuccessListner but the problem is after initializing in class, still it is null.
public User getUserDetails(String user_id){
collectionReference.document(mAuth.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
User= documentSnapshot.toObject(User.class);
}
});
return User; // returning null value
}