Home > other >  How to avoid creating multiple docs in firebase everytime user update his profile info in flutter
How to avoid creating multiple docs in firebase everytime user update his profile info in flutter

Time:07-12

So I just created a form for the user to key in his/her personal info in flutter. This is the code as a begining:


import 'package:cloud_firestore/cloud_firestore.dart';

class InsertProfInfoToDB {
  String? name;
  int? height;
  int? weight;
  String? gender;
  String? birthDateInString;
  InsertProfInfoToDB();

  Map <String, dynamic> toJson() => {'name': name, 'height': height,
    'weight' : weight, 'gender' : gender,
    'birthDateInString' : birthDateInString};

  InsertProfInfoToDB.fromSnapshot(snapshot)
      : name = snapshot.data()['name'],
        height = snapshot.data()['height'],
        weight = snapshot.data()['weight'],
        gender = snapshot.data()['gender'],
        birthDateInString = snapshot.data()['birthDateInString'];




}

 Future<void> submitForm () async {

 String userID = FirebaseAuth.instance.currentUser!.uid;

   int height =int.parse(heightController.text);
   int weight =int.parse(weightController.text);

   _insertProfInfoToDBToDB.name = nameController.text;
   _insertProfInfoToDBToDB.birthDateInString = birthDateInString;
   _insertProfInfoToDBToDB.height = height;
   _insertProfInfoToDBToDB.weight = weight;
   _insertProfInfoToDBToDB.gender = select;

   await FirebaseFirestore.instance
         .collection('users')
         .doc(userID)
         .collection('personalInfo')
         .add(_insertProfInfoToDBToDB.toJson());
 }

With this code, everytime I submit the form, a new doc will be created with the user info. How can I make it so that if the user info is exicted on the DB then update the new values to the same doc.

I tried to specify the name of the doc where the info is saved but I got an error saying

The method 'add' isn't defined for the type 'DocumentReference'.

CodePudding user response:

you can use update method

await FirebaseFirestore.instance
         .collection('users')
         .doc(userID).update();

CodePudding user response:

Since I had a collection that contains docs which contains collections, It was a bit tricky to get it done but here is how I did it. First I created this function to get the doc ID and added it to initState with help from this answer https://stackoverflow.com/a/67857128/18205996.

Future <void> getID () async {
  var collection = FirebaseFirestore.instance.
  collection('users').doc(userID).collection('personalInfo');
  var querySnapshots = await collection.get();
  for (var snapshot in querySnapshots.docs) {
    documentID = snapshot.id;
    print(documentID);
  }
}

Then the the Update function:

 Future<void> submitForm () async {
    int height =int.parse(heightController.text);
    int weight =int.parse(weightController.text);

    _insertProfInfoToDBToDB.name = nameController.text;
    _insertProfInfoToDBToDB.birthDateInString = birthDateInString;
    _insertProfInfoToDBToDB.height = height;
    _insertProfInfoToDBToDB.weight = weight;
    _insertProfInfoToDBToDB.gender = select;

    await FirebaseFirestore.instance
          .collection('users')
          .doc(userID).collection('personalInfo').doc(documentID)
        .update(_insertProfInfoToDBToDB.toJson());

  }
  • Related