i have this code " StreamBuilder<List<RideRecord>>( stream: queryRideRecord(…
- User
i have this code " StreamBuilder<List>( stream: queryRideRecord( queryBuilder: (rideRecord) => rideRecord .where( 'driver_uid', isEqualTo: currentUserReference?.id, ) .where( 'tripcompleted', isEqualTo: true, ) .where( 'time_accepted', isEqualTo: _model.calendarSelectedDay?.start, )," this fluuter code quers firebase. however its not doing what i want which is to only get the that date selected data, not previose dates or future dates. my _model.calendarSelectedDay?.start can have both _model.calendarSelectedDay?.start and _model.calendarSelectedDay?.end
- Assistant
To filter your Firebase query to retrieve only the records for the selected date, you need to perform a range query on the
time_acceptedfield. The issue with your current code is that you're usingisEqualTowith_model.calendarSelectedDay?.start, which likely includes a specific time (not just the date). This means it will only match records that have the exact same timestamp, including hours, minutes, and seconds, which is probably not what you want.Here's how you can modify your query:
-
Use a Range Query: Instead of
isEqualTo, useisGreaterThanOrEqualToandisLessThanOrEqualToto define the start and end of the selected day. -
Define Start and End of the Day: Ensure that your
_model.calendarSelectedDay?.startand_model.calendarSelectedDay?.endrepresent the very beginning and very end of the selected day.
Here's the modified code:
StreamBuilder<List<RideRecord>>( stream: queryRideRecord( queryBuilder: (rideRecord) => rideRecord .where( 'driver_uid', isEqualTo: currentUserReference?.id, ) .where( 'tripcompleted', isEqualTo: true, ) .where( 'time_accepted', isGreaterThanOrEqualTo: _model.calendarSelectedDay?.start, ) .where( 'time_accepted', isLessThanOrEqualTo: _model.calendarSelectedDay?.end, ),Important Notes:
-
Ensure Correct Date Range: Make sure that
_model.calendarSelectedDay?.startis set to the very start of the selected day (e.g.,2023-10-15 00:00:00) and_model.calendarSelectedDay?.endis set to the very end of the day (e.g.,2023-10-15 23:59:59). -
Avoid Overlapping Ranges: If your
time_acceptedfield is stored as a timestamp, ensure that the end range is exclusive if necessary (useisLessThaninstead ofisLessThanOrEqualToif you don't want to include records exactly at the end time). -
Indexing in Firestore: Remember that Firestore requires composite indexes for multiple range queries on different fields. Since you're performing a range query on
time_acceptedand equality queries on other fields, you should composite indexdriver_uid,tripcompleted, andtime_accepted. Firestore will prompt you with an error message containing a direct link to create the required index if it doesn't exist.
By adjusting your query to use a range based on the selected date, you'll retrieve only the records that fall within that specific day, excluding records from previous or future dates.
-