This is the getUserId and my function to count data that required the userId:
String get getuserId => AuthService.firebase().currentUser!.id;
Future<AggregateQuerySnapshot> myCollectionPrayer({required String userId}) =>
FirebaseFirestore.instance
.collection('record_prayer')
.where('dailyPrayerStatus', isEqualTo: 'Prayed On Time')
.count()
.get();
This is where I'm using the FutureBuilder
`<AggregateQuerySnapshot>`:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Count Firebase Documents'),
),
body: FutureBuilder<AggregateQuerySnapshot>(
future: myCollectionPrayer(userId: getuserId),
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Text('oops');
}
if (snapshot.connectionState == ConnectionState.done) {
int docCount = snapshot.data!.count;
return Text(
docCount.toString(),
style: Theme.of(context).textTheme.displayLarge,
);
}
return const CircularProgressIndicator();
},
),
this my screenshot for firebase enter image description here
There is no error on the code, but the display count for all user recorded data.
CodePudding user response:
If you want to be able to show the on-time aggregate for each individual user, you need to somehow associate each document in record_prayer
with each user. A common way to do that is to store the UID of that user in the document, for example in a user
field.
Once you do that, you can get only the prayers for the current user by using a query with a second where
clause:
FirebaseFirestore.instance
.collection('record_prayer')
.where('dailyPrayerStatus', isEqualTo: 'Prayed On Time')
.where('user', isEqualTo: getuserId) //