Search code examples
javaandroidfirebasegoogle-cloud-firestore

How to get a list of Strings from inside of a document Firestore


I have stored a list of Strings inside a document.i have given it a name("name") now i am trying to get it on the client side but do not know how to do it exactly. i can see that if my field is of type number i can use getLong("Fieldname") but i am unable to figure it out how to get back a list of type Strings. My code so far

FirebaseFirestore getlistofbrands=FirebaseFirestore.getInstance();
getlistofbrands.collection(FireStoreConstants.COL_BRANDNAMESONLY).document("Brands").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        List<String> listofbrandsfromfirestore=queryDocumentSnapshots.getDocuments().toArray(String.class);

also tried this

List<String> listofbrandsfromfirestore=queryDocumentSnapshots.toObjects(String.class);

how i uploaded the data

   Map < String, Object > docData = new HashMap < > ();
   List < String > deviceNameList = new ArrayList < String > ();
   deviceNameList.add("brand1");
   deviceNameList.add("brand2");
   deviceNameList.add("brand3");
   deviceNameList.add("brand4");
   docData.put("name", deviceNameList);
  firebaseFirestore.collection("FireStoreConstants.COL_BRANDNAMESONLY")
  .document("Brands").set(docData).addOnSuccessListener(new 
  OnSuccessListener < Void > () {
  @Override
  public void onSuccess(Void aVoid) {
  Log.d(TAG, "Repository:onSuccess:");
  }
  }).addOnFailureListener(new OnFailureListener() {
  @Override
  public void onFailure(@NonNull Exception e) {
  Log.d(TAG, "Repository:error" + e.toString());
  }
   });

Solution

  • Since you store the brand names in a single document, I doubt QuerySnapshot is the right class to get back. As far as I can see, you'll want to use getData() to get the Map<String, Object> that Firestore uses for your List:

    getlistofbrands
      .collection(FireStoreConstants.COL_BRANDNAMESONLY)
      .document("Brands")
      .get()
      .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.d(TAG, "DocumentSnapshot data: " + document.getData());
    
                    Map<String, String> brandsMap = (Map<String, String>)document.getData().getData("AllBrands");
                    List<String> brands = new LinkedList<String>();
    
                    for (String key: brandsMap.keySet()) {
                        brands.add(brandsMap.get(key));
                    }
    
                    // TODO: use the brands list
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
      });
    

    You might also want to try:

    List<String> brands = brandsMap.value();