简体   繁体   中英

How do I read data from a single document in flutter firebase base once (not in real time)?

I was wondering how I can read from a firebase database once. I want to be able to read from a document that has the same ID as the user that's logged in, via the firebase authentication, and then return the string of the user's name. Right now I am getting 3 errors on line 12 under the get method

  1. Function expressions can't be named
  2. Expected an identifier
  3. The getter '(' isn't defined for the type 'DocumentRefence<Object?>'
class DatabaseService {
  final CollectionReference usersCollection = FirebaseFirestore.instance.collection("users");

  final String uid;

  DatabaseService(this.uid); 

  Future<String> getUserName() async {
    String name = '';

    var document = await usersCollection.doc(uid);
    document.get() => then((doc) {
      name = doc('name');
    });

    return name;
  }
}

async/await should use of api calls not on the database path reference.

Just make sure of the snapshap.data() is correct, otherwise you can use my code as a reference.

Future<String> getUserName() async {
    var document = usersCollection.doc(uid);
    var snapshot = await document.get();
    Map<String, dynamic> data = snapshot.data();
    name = data['name'];
    return name;
}

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