Home > database >  Add collection on Firebase flutter
Add collection on Firebase flutter

Time:01-16

Inside the document that I show, I would like to create a collection called workers and inside it create another document.

enter image description here

My problem is that I make the following code sending the id of the document and it doesn't create anything.

void crearWorkerColeccion(iddeldocumento) {
  print('DENTRO ${iddeldocumento}');
  final String documento = iddeldocumento;
   FirebaseFirestore.instance
      .collection('Company')
      .doc('kGCOpHgRyiIYLr4Fwuys')
      .collection('WorkingDays')
      .doc(documento)
      .collection('trabajadores')
      .doc(FirebaseAuth.instance.currentUser!.toString());
}

CodePudding user response:

You can't create collection or document without any data into i.e You need to set/insert data into your document first time and that time it will create the collection>document>data

String datetime = DateTime.now().toString();
void crearWorkerColeccion(iddeldocumento) {
  print('DENTRO ${iddeldocumento}');
  final String documento = iddeldocumento;
   FirebaseFirestore.instance
      .collection('Company')
      .doc('kGCOpHgRyiIYLr4Fwuys')
      .collection('WorkingDays')
      .doc(documento)
      .collection('trabajadores')
      .doc(FirebaseAuth.instance.currentUser!.toString()).set({"workingDate"datetime});
}

I went through the screenshot you provided of your database,it's look like you want to add time there in that document so i set that.

CodePudding user response:

I think you have to add some data in the new collection "trabajadores", and for the last document id, maybe you have to add some thing for the currentUser, like: (.email or .displayName or blabla..).

And in general, all operation that related to FireStore it will be in the Future, so you have to add (async, await):

Try this:

Future<void> crearWorkerColeccion(iddeldocumento) async{
  print('DENTRO ${iddeldocumento}');
  final String documento = iddeldocumento;
   await FirebaseFirestore.instance
      .collection('Company')
      .doc('kGCOpHgRyiIYLr4Fwuys')
      .collection('WorkingDays')
      .doc(documento)
      .collection('trabajadores')
      .doc(FirebaseAuth.instance.currentUser!.toString()).set({
         'id': const Uuid().v4(),
         'name': nameController.text,
         'email': FirebaseAuth.instance.currentUser!.email,
   });
}
  • Related