Hasini
Hasini

Reputation: 337

Cannot Retrieve a Single data from Firebase

I want to retrieve the display name of the user from firebase database. But this code does not working. Is there any wrong with my code? Please help me.

 Firestore.instance.collection('/users').where('email', isEqualTo: '${widget.user.email}')
    .snapshots()
    .listen((data) =>
    data.documents.forEach((doc)=>userName==(doc["displayName"]),));

Upvotes: 1

Views: 62

Answers (1)

Aneesh Jose
Aneesh Jose

Reputation: 385

I thing there is a problem with assigning the value to userName. Try using:

Firestore.instance.collection('/users').where('email', isEqualTo: '${widget.user.email}')
    .snapshots()
    .listen((data) =>
    data.documents.forEach((doc)=>userName=(doc["displayName"]),));

Also, as @Ryosuke suggested, if you want to read data only once, you can use

Firestore.instance.collection('/users').where('email', isEqualTo: '${widget.user.email}')
.getDocuments().then((value){
value.documents.forEach((doc)=>userName=doc["displayName"]);
});

Hope this helps. Please let me know if something went wrong.

Upvotes: 1

Related Questions