Reputation: 28
I would like to know how I can get the value of the post which is 4 from the realtime database in flutter. I tried this and it is not working.
Future<Query> querystats() async {
var user = await firebaseauth.currentUser();
_userid = user.uid;
return FirebaseDatabase.instance.reference().child("stats").child("another");
}
querystats().then((query) {
query.once().then((snapshot) async {
var snapshot = await query.once();
var result = snapshot.value.values as Iterable;
for (var item in result) {
print(item['posts']);
}
][2]][2]
[]1
Upvotes: 0
Views: 88
Reputation: 598817
If you just want to read the value of a single property, you don't need the loop and Iterable
.
It can be something like:
var user = await firebaseauth.currentUser();
_userid = user.uid;
var query = FirebaseDatabase.instance.reference().child("stats/another/posts");
var snapshot = await query.once()
var result = snapshot.value;
print(result);
The changes I made:
child
, which saves some characters.await
on the call to once()
, so that you don't need a then
block.I didn't run the code, but this should be pretty close.
Upvotes: 2