Elias Marrero
Elias Marrero

Reputation: 197

Updating User Data in Cloud Firestore

I've been having a problem trying to update data from a logged in user. I have the uid, but there has to be a connection between the uid and the collection of users, so that the program picks the right user to update, but I don't know how to make it.

Here's what I have:

FirebaseUser loggedInUser;
final _firestore = Firestore.instance;
//
double _latitude;
double _longitude;

void getCurrentLocation() async {
  try {
    Position position = await Geolocator()
      .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    setState(() {
      _latitude = position.latitude;
      _longitude = position.longitude;
    });
    _firestore
      .collection('users')
      .document('${loggedInUser.uid}')
      .updateData({'location': GeoPoint(_latitude, _longitude)});
  } catch (e) {
      print(e);
  }
}

Here's what I've been getting:

E/flutter ( 9187): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update: projects/app-#####/databases/(default)/documents/users/CGyByl58ELc0zirlVjJpv5OWAc42, null)

So it is using the right uid ("CGyByl58ELc0zirlVjJpv5OWAc42")

Here's a screenshot from the Authentication tab of Firebase: enter image description here

But what I'm trying to get is the name of the collection of this user in the database: enter image description here

Upvotes: 1

Views: 2192

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

The user id is different than the document id, that's why you get that error since no document exists with the userId. You need to use the userId as the document id:

void addUser() async{
  var firebaseUser = await FirebaseAuth.instance.currentUser();
  Firestore.instance.collection("users").document(firebaseUser.uid).setData(
  {
    "age" : 38,
  }).then((_){
    print("success!");
  });
}

Now you will have the userId as the document id and you can update the document using the userId

Upvotes: 1

Related Questions