Musthafa
Musthafa

Reputation: 97

How to filter a Firebase list of object contains timestamp in flutter based on start and end DateTime?

I have an application in which I'm getting information from the firebase firestore database. This snapshot contains name, co-ordinates, timestamp of the captured image.

In the application, I would like to filter this snapshot based on the start and end date of the DateTime object. My issue is from firebase I have timestamps but from the flutter app I have DateTime objects. How can I filter the snapshot with these DateTime objects?

I'm relatively new to dart and getting very confused about this problem.

MY CODES.

Example of a snapshot

[{capture_date: Timestamp(seconds=1622809815, nanoseconds=371267000), 
image_path: firebasestoragepath, latitude: 11.3526445, 
id: 1SjdRL9VREdfcddsdl, field_name: NameofField, longitude: 75.892257},
{capture_date: Timestamp(seconds=1622829894, nanoseconds=221161000), 
image_path: firebasestoragepath, latitude: 11.3523268, 
id: 3ChWo7asdefuy0c7, longitude: 75.8921225, field_name: MyField}]

And I'm using flutter-datetime-picker to get from and to date objects. Please check the below code to filter the dates between the two.

print(from) 
2020-06-01 00:00:00.000
print(end)
2020-06-05 00:00:00.000

And the function to filter the snapshot is,

  _getSelectedSnaps(from, to) {
    DateTime startDate = from;
    DateTime endDate = end;
    List<DateTime> filteredSnapshot = [];
    
    for(int i = 0; i < snapshot; i++){
      //Here I want to filter based on from, end DateTime and firebase Timestamp

      //Push the map which passes between condition
    }
  }

Thanks in advance!

Upvotes: 0

Views: 986

Answers (1)

Santiago Curvello
Santiago Curvello

Reputation: 182

Use this to convert from timestamp to date

DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

After that use this methods isBefore() and isAfter() to filter.

Upvotes: 1

Related Questions