【问题标题】:How to sort by date in flutter List.generate?如何在flutter List.generate中按日期排序?
【发布时间】:2022-11-14 08:40:27
【问题描述】:

在 List 组件中,我们有这个方法。

 List<Widget> generateList(goalList) {
    return List.generate(
      goalList.length,
      (int index) {
        return GoalCardBase(
          goalId: goalList.id,
          child: GoalCardData(goal: goalList[index]),
        );
      },
    );
  }

我正在尝试按 startDate 对它们进行排序。我正在寻找一个解决方案,每个人都提出类似的建议。 目标列表.sort((a, b)=> a.compareTo(b['objectiveStartDate'])); 但从goalList 我无法到达开始日期。也许我完全错了。 如果你要解决这个问题,你会怎么做?

整个代码



import 'package:app/src/common.dart';
import 'package:app/src/common_widgets/app_buttons.dart';
import 'package:app/src/features/goals/card/goal_card_base.dart';
import 'package:app/src/features/goals/card/goal_card_data.dart';
import 'package:app/src/utils/utils.dart';
import 'package:go_router/go_router.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

class GoalsPageBodyGoals extends StatelessWidget {
  final List<Widget> data;

  const GoalsPageBodyGoals({Key? key, required this.data}) : super(key: key);

  List<Widget> generateList(goalList) {
    return List.generate(
      goalList.length,
      (int index) {
        return GoalCardBase(
          goalId: goalList.id,
          child: GoalCardData(goal: goalList[index]),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      ...data,
      const SizedBox(height: 24),
      Column(
        key: const Key('goals_page_action_buttons'),
        children: [
          AppButtons.button(
              onPressed: () => context.go('/add-goal'),
              child: Text(context.l10n.goalsListPrimaryButtonText)),
          AppButtons.textButton(
              onPressed: () => AppUtils.snackBarNotImplemented(context),
              phosphorIcon: PhosphorIcons.arrowRight,
              child: Text(context.l10n.goalsListTextLink))
        ],
      ),
    ]);
  }
}

在目标目标(GoalCardData(目标:goalList[index]),) 我有属性(title、startDate、amount、salary 等),所以当我们创建一个目标时,它有日期。但是当我列出它们时,它只是随机保存。

【问题讨论】:

    标签: flutter list sorting date widget


    【解决方案1】:

    在代码中呈现/返回之前尝试对列表进行排序。

    goalList.sort((b, a) =&gt; a['startDate'].compareTo(b['startDate']);

    【讨论】:

      【解决方案2】:

      这是我在 dartpad 上尝试过的代码,它可以工作:

      void main(){
        cls _cls = cls("title",DateTime.now());
        cls cls2 = cls("title2",DateTime.now().subtract(Duration(days:1)));
        
        List<cls> clist = [_cls,cls2];
        for(var v in clist){
          print(v.startDate.toString());
        }
        
        clist.sort((a,b) => a.startDate!.compareTo(b.startDate!));
        
        for(var v in clist){
          print(v.startDate.toString());
        }
      }
      
      class cls{
        String? title;
        DateTime? startDate;
        
        cls(this.title,this.startDate);
      }
      

      所以,在你的生成列表函数,在返回之前,你可以这样做:

      goalList.sort((a,b) => a.startDate.compareTo(b.startDate));
      

      【讨论】:

        猜你喜欢
        • 2021-01-12
        • 2012-03-20
        • 2012-10-09
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 2013-01-14
        相关资源
        最近更新 更多