【问题标题】:Show list items based on date - Flutter根据日期显示列表项 - Flutter
【发布时间】:2021-04-01 06:04:30
【问题描述】:

当每个列表项的日期等于今天的日期时,我想要一个列表。 我个人在未来的 builder 中将这段代码写在 listview.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;
            }
          }

此代码仅适用于第一项。它没有显示任何其他项目。

【问题讨论】:

    标签: flutter listview sqflite flutter-listview flutter-futurebuilder


    【解决方案1】:

    我解决了。问题在于 else 返回类型为空。我给了它一个空大小的盒子,它可以工作。

    else {
                          return SizedBox.shrink();
                        }
    

    【讨论】:

      【解决方案2】:

      试试这个

      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();
      }
      

      【讨论】:

      • 再说一次,它只适用于第一项。
      • 我又检查了一遍;只要我不插入具有不同日期的项目,它就可以工作,但是当我有一个具有例如明天日期的项目时,不会显示任何内容。
      • 但你在问题中明确提到I want to have a list when the date of each list item is equal to todays date
      • 是的,但您和我的代码都不会显示。每当我添加一个日期不是今天日期的项目时,它就会停止列表。
      • 发布您的 ListView 或整页的完整代码
      猜你喜欢
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2015-04-21
      • 2021-12-29
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多