1

I am trying to add a subcollection to the main collection in this case users. I want that every user can add their own medication and it store in Users => medication.

User collection
medication subcolletion

this is the collection of users-

Future addUserDetails(String name, String surname, String email, int age,
      int weight, String gender, int phonenumber) async {
    await FirebaseFirestore.instance.collection('users').add({
      'name': name,
      'surname': surname,
      'email': email,
      'age': age,
      'weight': weight,
      'gender': gender,
      'phonenumber': phonenumber
    });

This is the collection for medication - (in a different page)

SizedBox(height: 4),
                      OutlinedButton(
                        onPressed: () {
                          Map<String, dynamic> data = {
                            'Triggers': _triggersController.text +
                                (', ') +
                                _triggersController2.text,
                            'Preventer inhaler': _preventController.text,
                            'Reliever inhaler': _relieverController.text,
                            'Other medication': _othermedicationController.text
                          };
                          FirebaseFirestore.instance
                              .collection('Medication')
                              .add(data);
                        },
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
DARIO
  • 35
  • 6
  • Does this answer your question? [how to add sub collection to a document in firestore?](https://stackoverflow.com/questions/48873465/how-to-add-sub-collection-to-a-document-in-firestore) – Mises Jul 30 '22 at 11:27
  • This won't help you too. https://stackoverflow.com/questions/54176235/creating-sub-collection-in-document-from-flutter-app – Mises Jul 30 '22 at 13:56
  • Hey DARIO. Given that the link Mises shared shows an example of adding a document to a subcollection, can you clarify what the problem is that you have implementing that for your own use-case? Is there an error message? – Frank van Puffelen Jul 30 '22 at 14:02
  • Hello guys i am quite new in programming and sometimes is difficult to understand where to add a piece of code . I added medication collection code with the firebase instance. – DARIO Aug 01 '22 at 14:09

0 Answers0