0
votes

I'm having an issue with retrieving certain data from Firebase and i tried it many ways. I want to get for instance, city data from thee current user. Instead of getting city name, I get 'Query snapshot' value on city watch list.

  void addToFb() async {
final User user = FirebaseAuth.instance.currentUser;
final uid = user.uid;
final city = await FirebaseFirestore.instance
    .collection('Users')
    .doc(uid)
    .collection("city")
    .get();


dbRef.push().set({
  "name": nameController.text,
  "author": authorController.text,
  "city": city,
  "category": dropdownValue,
  "publisher": publisherController.text,
  "years": yearsController.text,
  "info": infoController.text,
  "ownerid": uid
2
Your code is writing to the Realtime Database, but it is reading from Cloud Firestore. While both databases are part of Firebase, they're completely separate, have their own APIs, and data from one doesn't show up in the other. - Frank van Puffelen
If i understand correctly @frank, i need to change FirebaseFirestore into FirebaseData? - Tomee
You need to change one of them, so that reading and writing happen with the same database. Which one that is, is up to you to decide. If you need help choosing, consider firebase.google.com/docs/database/rtdb-vs-firestore - Frank van Puffelen

2 Answers

2
votes

You need to first determine if you're using the Realtime Database or Firestore.

FirebaseDatabase refers to the Firebase Realtime Database and doesn't have collections. Its usage would look like this:

FirestoreDatabase().reference('Users')

FirebaseFirestore is probably what you're looking for as it refers to Firestore and has collections.

FirebaseFirestore.instance.collection('Users')

2
votes

Your code is looking for a collection called city under your user document. However, it sounds like you just need the city field within the document.

Try this:

final userDocument = await FirebaseFirestore.instance
    .collection('Users')
    .doc(uid)
    .get();
    
final userData = userDocument.data();
final city = userData['city'];