Calin Onaca
Calin Onaca

Reputation: 185

How to convert an object from Firestore to an array list?

When I create a user i have a List with the Urls of the images that that user needs.

Like

 final List<String> picturesUrls = pictureUrl; 

My user object is




import java.util.List;

public class User {


    public List<String> picturesUrls;


    public User() {

    }

    public User(List<String> picturesUrls) {

        this.picturesUrls = picturesUrls;


    }

}

It all work fine, but when I want to get those data from FireStore

  db.collection("Users").whereEqualTo("email",user.getEmail())
                             .get()
                             .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                 @Override
                                 public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                     if(task.isSuccessful()){
                                         for(QueryDocumentSnapshot document: task.getResult()){
                                            Object obj = document.get("picturesUrls");
                                             actualUser = new User(              
                                                     picturesUrls
                                             );
                                         }
                                     }
                                 }
                             });

but In the User model, picturesUrls is the type of List and what gives me from FireStore is an object. For now in that list it is only 1 string. How can I convert that object to a List to create the new User Object and then loop into that List to get the last value ("actual profile picture")

Thank you in advance!

Upvotes: 1

Views: 1717

Answers (1)

Alif Hasnain
Alif Hasnain

Reputation: 1256

First you convert DocumentSnapshot to instance of User class. You would have to initialize getter in User class. Then you can use getter to get the url list. So you code will be like this :

  db.collection("Users").whereEqualTo("email",user.getEmail())
                             .get()
                             .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                 @Override
                                 public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                                    for(DocumentSnapshot ds : queryDocumentSnapshots)   {
                                        User user = ds.toObject(User.class);
                                        List<String> urlList = user.getPicturesUrls();
                                    }

                                }
                             });

Upvotes: 1

Related Questions