3

I want to upload data like this:

Image

Edited Questions

//I have ArrayList<FoodModel> arr;
ArrayList<FoodModel> arr = new ArrayList<>();
arr.add(new FoodModel("Hamburger"));
arr.add(new FoodModel("Pizza"));
arr.add(new FoodModel("Chicken"));
//and so on...

I want to upload that ArrayList to firestore it is Possible?

0

1 Answer 1

1

You just need add a list field in the hashmap:

Map<String, Object> docData = new HashMap<>();
docData.put("favFoods", Arrays.asList("Hamburger", "Vegetables"));

db.collection("data").document("one")
        .set(docData)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "DocumentSnapshot successfully written!");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Error writing document", e);
            }
        });

Alternatively, you can also use arrayUnion to add new items to an array.

DocumentReference docRef = db.collection("col").document("docId");
docRef.update("favFoods", FieldValue.arrayUnion("Pizza"));

You can add array of objects (and even nested maps) in Firestore. However, you should add the array as a list.

References:

Sign up to request clarification or add additional context in comments.

7 Comments

Hello, @Dharmaraj I updated my question please check it out.
@TonyStark you can add an array of objects... just put that arr in the hashmap. iirc you need to convert it to a list as in the documentation.
can you please edit your answer as you say in the comment.
Updated the answer 👍🏻.
if I want to access it how can I do that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.