简体   繁体   中英

Firestore data not getting stored in Flutter

I'm trying to store the user data in firestore, when I'm using the phone number as the document id it's getting stored perfectly but as soon as I changed it to uid it's not. And there were no exceptions been thrown at either. Also I checked if the uid is empty or not too and It's not empty either.

Future addUserToFirestore(String uid) async {
    final docUser = FirebaseFirestore.instance
        .collection('users')
        .doc(uid);

    final user = u.User(
        name: widget.fullName,
        email: widget.email,
        gender: widget.gender,
        nic: widget.nic,
        phoneNumber: widget.phone,
        bloodType: widget.bloodType,
        dateOfBirth: widget.dateOfBirth,
        address: widget.address,
        age: int.parse(widget.age));

    final json = user.toJson();
    await docUser.set(json);
  }

When I use .doc('+94${widget.phone}'); instead of .doc(uid); it works fine. But I want to use the uid as document id. Is there a way to get this done?

Try the following method. Hopefully, this will help.

   Future<void> storeUser () async {
     var instance = FirebaseFirestore.instance;
     await instance.collection("users").doc("uid").set({
       // add all of the user data here.
     });
   }
  

Else also try:

Future addUserToFirestore(String uid) async {
    // add await here as well

    final docUser = await FirebaseFirestore.instance
        .collection('users')
        .doc(uid);

    final user = u.User(
        name: widget.fullName,
        email: widget.email,
        gender: widget.gender,
        nic: widget.nic,
        phoneNumber: widget.phone,
        bloodType: widget.bloodType,
        dateOfBirth: widget.dateOfBirth,
        address: widget.address,
        age: int.parse(widget.age));

    final json = user.toJson();
    await docUser.set(json);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM