How to get an array from Firestore?

I have the data structure illustrated below stored in Cloud Firestore. I want to save the dungeon_group which is an array of strings stored in Firestore.

I have difficulty in getting the data and stored as an array. I am just able to get a weird string but any method to store as a string array? Below is the code I used.

I am able to achieve this in Swift as follow, but not sure how to do the same in Android.

enter image description here


Firestore.firestore().collection("dungeon").document("room_en").getDocument { 
    (document, error) in
    if let document = document {
        let group_array = document["dungeon_group"] as? Array ?? [""]

Java Android:

                     OnCompleteListener<DocumentSnapshot>() {
                     public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                         DocumentSnapshot document = task.getResult();
                         String group_string= document.getData().toString();
                         String[] group_array = ????
                         Log.d("myTag", group_string);

Console output as the follow:

{dungeon_group=[3P, Urgent, Mission Challenge, Descended, Collaboration, Daily, Technical, Normal]}


When you call DocumentSnapshot.getData(), it returns a Map. You’re just calling toString() on that map, which is going to give you a dump of all the data in the document, and that’s not particularly helpful. You need to access the dungeon_group field by name:

DocumentSnapshot document = task.getResult();
List<String> group = (List<String>) document.get("dungeon_group");
  • edit: syntax error in typecasting

Leave a Reply

Your email address will not be published. Required fields are marked *