2

I have question collection and some 'X' number of fields in that, but now I want to set an array of key value pairs in existing document of my firestore collection question for storing the data in my firestore the desired structure is shown below in image, I am not able to get how to create structure like this I want to fetch them and create data in firestore using flutter and dart,

so far I am able to set string fields like this but not array of them,

 Future<void> setAnswer() async {
      String ans = ans_Controller.text;
      FirebaseFirestore db = FirebaseFirestore.instance;

      await db.runTransaction((transaction) async {
        DocumentReference qRef =
            db.collection('question').doc(widget.question.id);
        await transaction.set(
            qRef,
            {
              'content': ans,
              'timestamp': DateTime.now().toString(),
              'username': _currentUser.displayName,
            },
            SetOptions(merge: true));
      });
    }

and also every array element must have same key but different values, like a hashmap

enter image description here

any help is appreciated,

thank you

2
  • What is the problem when you run this code? Commented Oct 4, 2021 at 22:47
  • there's no problem in that code but that's not giving me the desired result as its for the single string and not for array of maps which I want please check the screenshot I have attached I want to do something like that how its possible to do so, I just want to append all the three fields in single array and not in one separate string fields as I have done in the given code above Commented Oct 4, 2021 at 22:57

1 Answer 1

1

I think you're looking for the array-union operation. To add an element to the answer array if it doesn't exist in there yet, do:

documentRef.updateData({'answer': FieldValue.arrayUnion([{
  'content': ans,
  'timestamp': DateTime.now().toString(),
  'username': _currentUser.displayName,
}])});
Sign up to request clarification or add additional context in comments.

7 Comments

and how can I read all of the three in separate widget once I have updated them?
okay I gotcha how to create one array-union but how to read it, would be very helpful if you you can show how can we read all those three fieldvalues and show them in text widget
That's a new problem, but it could be something like this: stackoverflow.com/questions/50808513/… or this: stackoverflow.com/questions/54012067/…
the solutions suggested by you for reading data were using maps and I dont want to use maps for just retrieving three fieldvalues is there any other way to do so like creating a future then storing a value inside it and make it accessible outside the future function
I just updated this question to include reading part of array-union also now you can suggest me some methods or way to read those three values
|

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.