在List组件中我们有这个方法。
List<Widget> generateList(goalList) {
return List.generate(
goalList.length,
(int index) {
return GoalCardBase(
goalId: goalList.id,
child: GoalCardData(goal: goalList[index]),
);
},
);
}
我正试图按开始日期排序。我在寻找一个解决方案,每个人都有类似的建议。goalList。排序((a, b) =比;a.compareTo (b [' objectiveStartDate ']));但从进球名单上看,我没能赶上开赛日期。也许我完全错了。如果你要解决这个问题,你会怎么做?
整个代码
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]),)我有属性(标题,开始日期,金额,工资等),所以当我们创建一个目标时,它有日期。但是当我列出它们时,它只是随机保存。
在代码中呈现/返回列表之前尝试排序。
goalList.sort((b, a) => a['startDate'].compareTo(b['startDate']);
这是我在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);
}
在generateList中函数,在返回之前,你可以这样做:
goalList.sort((a,b) => a.startDate.compareTo(b.startDate));