1

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();
      },
    ),

There is no error on the code, but the display count for all user recorded data.

this my screenshot for firebase

enter image description here

Jay
  • 34,438
  • 18
  • 52
  • 81
  • what is AggregateQuerySnapshot in your code, and what does the count() in your method – Gwhyyy Dec 26 '22 at 14:59
  • However, I can clearly see that the `myCollectionPrayer` do take a `user` as an argument but it's unused inside the method – Gwhyyy Dec 26 '22 at 15:00
  • I just follow the video on youtube how to cound data https://www.youtube.com/watch?v=5e_Tir8ot1c&ab_channel=ExtraServingsBTS, I just copied and added to count only for current user recorded data. – Syahir Isyraf Dec 26 '22 at 15:03
  • @Gwhyyy can you explained more details please. Sorry this is my first time learning flutter – Syahir Isyraf Dec 26 '22 at 15:06
  • oh sorry, I know now what it is, can you explain what you want to achieve in your code exactly – Gwhyyy Dec 26 '22 at 15:11
  • let say i have 2 users. So each user need to record their daily prayers. For example, user 1 want to record zohor prayer, so user 1 click the button zohor prayer and it will display showModalBootmSheet and inside the modal have three button which is button 'prayed on time', 'prayed late' & 'not prayed'. when the user click the button 'prayed on time' it will in to the database firebase. So my goals is i want to count the 'prayed on time' for current user only. Let's user 1 record 5 time 'prayed on time' so it will count only for user 1. If user 2 record, it will count for user 2 only @Gwhyyy – Syahir Isyraf Dec 26 '22 at 15:20
  • 1
    I understand, can you include also a screenshot of your firestore database so I can tell you what you will need to do – Gwhyyy Dec 26 '22 at 15:31
  • @Gwhyyy i have added the screenshot above – Syahir Isyraf Dec 26 '22 at 15:50

1 Answers1

3

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) // 
     .count()
     .get();
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807