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

Mithson
  • 1,554
  • 13
  • 33
  • What is the problem when you run this code? – Frank van Puffelen Oct 04 '21 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 – Mithson Oct 04 '21 at 22:57

1 Answers1

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,
}])});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • and how can I read all of the three in separate widget once I have updated them? – Mithson Oct 05 '21 at 03:06
  • 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 – Mithson Oct 05 '21 at 03:49
  • That's a new problem, but it could be something like this: https://stackoverflow.com/questions/50808513/how-do-you-load-array-and-object-from-cloud-firestore-in-flutter or this: https://stackoverflow.com/questions/54012067/flutter-firestore-how-to-read-and-write-arrays-of-objects – Frank van Puffelen Oct 05 '21 at 03:52
  • 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 – Mithson Oct 05 '21 at 03:58
  • 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 – Mithson Oct 05 '21 at 04:03
  • Sorry, I'm having a hard time parsing this new problem - and given that your original problem of how to write the data has been answered, it's unlikely somebody else will provide further help. I recommend opening a new question for the problem with reading. Be sure to show you data structure there, and to show what you've tried already based on the links I provided. – Frank van Puffelen Oct 05 '21 at 13:05
  • I created this for reading the array now can you answer this https://stackoverflow.com/q/69445059/13984728 – Mithson Oct 05 '21 at 14:52