Home > Net >  How to sort by date in flutter List.generate?
How to sort by date in flutter List.generate?

Time:11-11

In List component we have this method.

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

i am trying to sort them by startDate. I was looking a solution everyone suggest something like that. goalList.sort((a, b)=> a.compareTo(b['objectiveStartDate'])); but from goalList I could not reach the start date. Maybe i am completly wrong. What would you do if you were to solve this problem?

The whole code



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))
        ],
      ),
    ]);
  }
}

IN goal Objective (GoalCardData(goal: goalList[index]),) I have attributes(title, startDate, amount, salary etc.) , so when we create a goal it has date. But when i list them its just randomly saving.

CodePudding user response:

Try sorting the list before rendering/returning it in the code.

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

CodePudding user response:

This is the code I tried on dartpad and it works:

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

So, in your generateList function, before returning, you can do this:

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