Home > other >  The instance member 'id' can't be accessed in an initializer
The instance member 'id' can't be accessed in an initializer

Time:09-25

//error shown

The instance member 'id' can't be accessed in an initializer.

//How can I get rid of the error??

I pass id value from other stateful widget, How can I now use id to initialize users

class AddUser  {
  final String answer;
  final String id;

  AddUser(this.answer,this.id);

  DocumentReference users = FirebaseFirestore.instance.collection('Questions').doc("ceb").collection("ceb").doc(id);}

CodePudding user response:

I believe the documents have enough resource to do what you're looking.

CollectionReference users = FirebaseFirestore.instance.collection('users');

 Future<void> addUser() {
 return users
  // existing document in 'users' collection: "ABC123"
  .doc('ABC123')
  .set({
    'full_name': "Mary Jane",
    'age': 18
  },
 
  .then(
  (value) => print("'full_name' & 'age' merged with existing data!")
   )
   .catchError((error) => print("Failed to merge data: $error"));
 }
  • Related