Home > Software design >  How to get the doc.length of today record in flutter firebase
How to get the doc.length of today record in flutter firebase

Time:02-01

I'm new in flutter firebase, how can i display the length of my Total sales sales record. I have the collection of SalesRecord and and a field name Created and i want to know how many sales i made during this Day.

My Code:

var todaySales = 0;
FutureBuilder(
    future: FirebaseFirestore.instance
        .collection('SalesRecord')
        // .orderBy('Created', descending: true | false)
        .where("Created", isGreaterThan: DateTime.now())
        .get()
        .then((myDocuments) {
      setState(() {
        todaySales = myDocuments.docs.length;
      });
      //print("${myDocuments.docs.length}");
    }),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        return const Center(
            child: CircularProgressIndicator());
      } else if (snapshot.hasError) {
        return const Center(
            child: CircularProgressIndicator());
      }
      return Text(
        todaySales.toString(),
        style: const TextStyle(
          fontSize: 50.0,
          fontWeight: FontWeight.bold,
        ),
      );
    }),

Code Update :-

FutureBuilder<QuerySnapshot<Map<String, dynamic>>>(
                        future: FirebaseFirestore.instance
                            .collection('SalesRecord')
                            .where("Created",
                                isGreaterThan: DateTime.now())
                            .get(),
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                            return Text(
                              snapshot.data!.docs.length.toString(),
                              style: const TextStyle(
                                fontSize: 50.0,
                                fontWeight: FontWeight.bold,
                              ),
                            );
                          } else if (snapshot.hasError) {
                            return const Center(
                                child: CircularProgressIndicator());
                          } else {
                            return CircularProgressIndicator();
                          }
                        }),

When i update the SalesRecord it change the value for a while but the record count back to 0 again by itself after 20sec

From Today Sales i want to count ho many sale i made during this day

CodePudding user response:

The reason for count getting back to 0 is that DateTime.now() gets the current date time i.e. if you made a sale 30 seconds ago it won't be shown as the current time is 30 seconds greater than previous one.

So declare a variable as shown below which will get us the today's date (2022-01-31 00:00:00.000) and zero time values to mark the beginning of the day.

DateTime dateToday = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day);

And now use this in your code as:-

FutureBuilder<QuerySnapshot<Map<String, dynamic>>>(
      future: FirebaseFirestore.instance
          .collection('SalesRecord')
          .where("Created",
          isGreaterThan: dateToday)
          .get(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Text(
            snapshot.data!.docs.length.toString(),
            style: const TextStyle(
              fontSize: 50.0,
              fontWeight: FontWeight.bold,
            ),
          );
        } else if (snapshot.hasError) {
          return const Center(
              child: CircularProgressIndicator());
        } else {
          return CircularProgressIndicator();
        }
      })

CodePudding user response:

You can do the following:

          FutureBuilder<QuerySnapshot<Map<String, dynamic>>>(
              future: FirebaseFirestore.instance
                  .collection('SalesRecord')
                  .where("Created", isGreaterThan: DateTime.now())
                  .get(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Text(
                    snapshot.data!.docs.length.toString(),
                    style: const TextStyle(
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                    ),
                  );
                } else if (snapshot.hasError) {
                  return const Center(child: CircularProgressIndicator());
                } else {
                  return CircularProgressIndicator();
                }
              }),

get() returns a Future<QuerySnapshot<Map<String, dynamic>>> so assign that to the future property. Then the snapshot of type AsyncSnapshot will contain the data of this future and you can access it inside if(snapshot.hasData) using the data property.

  •  Tags:  
  • Related