Reputation: 63
I'm currently creating a dashboard application for my main application, this dashboard is able to display in charts the demography of the users that uses the app. I use Firebase Database as the backend. The JSON tree of my DB is as shown below. My question is, how do I get the amount of data with a specific value of a key? Example: the number of children with the value 'Pria' for the key 'jk' is 2.
My Backend JSON Tree:
So far, I'm able to get all of the data using:
DatabaseReference itemRef = FirebaseDatabase.instance.reference().child('data_pengguna');
And I've also tried the codes below, but it doesn't seem to work:
int jmlPria;
FirebaseDatabase.instance
.reference()
.child('data_pengguna')
.orderByChild('jk')
.equalTo('Pria')
.once()
.then((onValue) {
Map data = onValue.value;
jmlPria = data.length;
});
But I haven't successfully filtered the data and put it inside a variable, can anyone help me?
Many thanks in advance.
Upvotes: 4
Views: 3700
Reputation: 599176
That last snippet looks correct, jmlPria
should have the number of children.
But the value of jmlPria
will only be set to the latest value inside the then()
callback. Make sure that Text(
$jmlPria)
is inside the then()
callback. Outside of that, jmlPria
will not have the correct value.
Also see Doug's great blog post on asynchronous programming.
Upvotes: 1