0

I want to have a list when the date of each list item is equal to todays date. I, personally, wrote this code in listview.builder in future builder:

          itemBuilder: (context, index) {
            var now = DateTime.now();
            var nowFormated = DateFormat.MMMd().format(now);
            DateTime time = DateTime.parse(snapshot.data[index].date);
            var databaseFormated = DateFormat.MMMd().add_Hm().format(time);
            var checkerFormated = DateFormat.MMMd().format(time);
            if (checkerFormated == nowFormated) {
              print (checkerFormated);
              print(nowFormated);
              return Todays(
                  snapshot.data[index].name != null
                      ? snapshot.data[index].name
                      : '',
                  databaseFormated);
            } else {
              return null;
            }
          }

this code only works for the first item. it doesn't show any further items.

Mahdi Aghajani
  • 127
  • 1
  • 1
  • 8

2 Answers2

0

Try this

itemBuilder: (context, index) {
  final now = DateTime.now();
  final time = DateTime.parse(snapshot.data[index].date);
  final databaseFormated = DateFormat.MMMd().add_Hm().format(time);
  return (now.year == time.year && now.month == time.month && now.day == time.day) ? Todays(snapshot.data[index].name != null ? snapshot.data[index].name : '', databaseFormated) : SizedBox();
}
dm_tr
  • 4,265
  • 1
  • 6
  • 30
0

I solved it. the problem was with the else return type was null. I gave it an empty sizedbox and it works.

else {
                      return SizedBox.shrink();
                    }
Mahdi Aghajani
  • 127
  • 1
  • 1
  • 8