1

Problem 1

  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

  1. 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
    }
Dark Knight
  • 31
  • 1
  • 6
  • Please limit yourself to a single question per post. For #2, there is no way to synchronously return something that is asynchronously loaded. The the whole point why the API returns a Task. See https://stackoverflow.com/questions/51000169/how-to-check-a-certain-data-already-exists-in-firestore-or-not/51002413#51002413, https://stackoverflow.com/questions/46706433/firebase-firestore-get-data-from-collection/46714740#46714740 and https://stackoverflow.com/questions/48499310/how-to-return-a-documentsnapshot-as-a-result-of-a-method/48500679#48500679 – Frank van Puffelen Feb 27 '21 at 15:45

0 Answers0