简体   繁体   中英

ArrayList add() method returns empty or blank firebase java

I am facing a strange situation. Believe me. New to using Firestore database. I have a collection as follow,

List<String> myStringList = new ArrayList<String>(); 
ApiFuture<QuerySnapshot> apiFutureResults = query.get();
QuerySnapshot querySnapshot = apiFutureResults.get();
List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
for(QueryDocumentSnapshot document: documents)
{
     log.debug(document.getString("myColumnName")); //prints value successfully
     String myString = document.getString("myColumnName"); // Same value assigned to String type.
     log.debug("Value of myString", myString);  //prints value successfully
     log.debug("Result is ::",myStringList.add(myString)); // prints empty or blank.
     log.debug("Arraylist size::", myStringList.size()); // Returns empty or blank.
     log.debug("Arraylist content::", myStringList); // Returns empty or blank  }

I was expecting true after addition. In my case I am getting myStringList.add(myString); returns empty . String is fetched from QueryDocumentSnapshot (com.google.cloud.firestore). In What scenarios ArrayList.add() method returns empty. Or Is there any issue with this approach to fetch records from Firestore . This is not android application. Just a Java product.

Your code is printing the value returned by myStringList.add() . I suspect that's not what you intended. Printing the result of add() doesn't seem very useful for debugging purposes.

Maybe what you wanted to do is first add the item to the list first, then print the contents of the list.

myStringList.add(myString);
log.debug("Result is ::",myStringList); // print the list

You probably also want to print the list after the loop completes, not every iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM