How to add new data to firebase

Viewed 46

How to add new data to firebase, in the picture on the second column there are users, and on the last one there is my note. This note whas created when user created account, and it whas updated when user logged in, before whas "bad location" etc. My problem is to add new note like this, not update it, kepp it, and at the same time, in the same column have some kind of "new collection" with the same 3 strings, but with different data.

enter image description here

class DataService {
  final String uid;
  DataService({required this.uid});
  final CollectionReference notesCollection =
      FirebaseFirestore.instance.collection('Notes');

  Future createUserData(String notes, String localisation, String title) async {
    return await notesCollection.doc(uid).set({
      'notes': notes,
      'title': title,
      'localisation': localisation,
    });
  }

  Future addData(String notes, String localisation, String title) async {
    return await notesCollection.doc(uid).set({
      'notes': notes,
      'title': title,
      'localisation': localisation,
    });
  }
}

This class shows my createUserData, when my user creates account or loggs in, but how to change "addData" in order to have logic as I described above?

1 Answers

if I understand correctly you want to create a new collection for each documents as history. try this:

notesCollection.doc(uid).collection("historic").add({
  'notes': notes,
  'title': title,
  'localisation': localisation,});
Related