LostTexan
LostTexan

Reputation: 891

Query Firestore by single date in .where section

Trying to retrieve a list of all events stored in Firestore collection. I think the issue is with the data used in the .where statement. Here is the query I am using:

_db.collection('agency').doc(globals.agencyId).collection(
            'event').where('eventDate', isEqualTo: _focusedDay).snapshots(),

_focusedDay = 2021-07-02 00:00:00.000Z the value in the document in Firestore = July 2, 2021 at 12:00:00 AM UTC-6.

The time portion is different between the two values so I am thinking this is causing the query to not return any records but this was working and I didn't change the query. I have made changes to fix errors else where in the code. Am I on the right track? How do I fix the dates so they match?

Here is the entire code for the StreamBuilder:

StreamBuilder<QuerySnapshot>(
        stream: _db.collection('agency').doc(globals.agencyId).collection(
            'event').where('eventDate', isEqualTo: _focusedDay).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
                child: const Text(
                  'Loading...',
                  style: TextStyle(
                      fontSize: 20, fontWeight: FontWeight.bold),
                ));
          } else {
            var doc = snapshot.data!.docs;
            return new ListView.builder(
                itemCount: doc.length,
                itemBuilder: (BuildContext context, int index) {
                  Event _event = Event.fromFirestore(
                      doc[index].data() as Map<String, dynamic>);
                  return ListTile(
                    isThreeLine: true,
                    title: Text(
                      '${_event.eventName ?? 'n/a'}',
                      style: TextStyle(
                          fontWeight: FontWeight.w900,
                          color: Colors.blueAccent),
                    ),
                    subtitle: Text.rich(TextSpan(
                        text:
                        '${DateFormat('EE MM-dd-yyyy').format(_event.eventDate!) ?? 'n/a'}\n'
                        '${DateFormat('h:mm a').format(_event.eventStartTime!) ?? 'n/a'}, '
                        'Duration: ${_event.eventDuration ?? 'n/a'} minutes',
                        children: <TextSpan>[
                          TextSpan(
                            text:
                            '\n${_event.eventDescription ?? 'n/a'}',
                            style: TextStyle(
                                fontWeight: FontWeight.w900,
                                color: Colors.blueGrey),
                          )
                        ])),
                    //trailing: Text('MLS#: ${_event.mlsNumber ?? 'n/a'}'),
                    onTap: () {
                      globals.newTrxn = false;
                      
                    },
                  );
                }
            );
          }
        },

      ),

Upvotes: 0

Views: 127

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598951

The Timestamp type stores a very accurate moment in time.

If you want to select an entire day of timestamps, you need to query for a range, like so:

where("eventDate", isGreaterThanOrEqualTo: startOfDay).where("eventDate", isLessThan: startOfNextDay)

There is no shorthand notation to do this with a single condition.

Upvotes: 1

Related Questions