Amr Gharieb
Amr Gharieb

Reputation: 17

I want to filter List of objects depend on Date

i stucked in filter list depend on date condition like what i make in c#

widget.VarData = widget.Data.where((x)=>x.Date. > DateTime.now().add(Duration(days: -7)));

it show me error that datetime class doesnot have >

any recommendation

thanks

Upvotes: 0

Views: 629

Answers (1)

lrn
lrn

Reputation: 71783

DateTime has isAfter and isBefore. Since DateTime has both a date/time and a time zone, it's not a single-dimensional value. That's why it doesn't use the simple < for comparison.

So:

var weekAgo = DateTime.now().subtract(const Duration(days: 7));
widget.varData = widget.data.where((x) => x.date.isAfter(weekAgo));

(It seemed wasteful to create a new DateTime object on each comparison).

Upvotes: 1

Related Questions