我做错了什么?当我尝试对_notes进行排序时,出现“构造函数的名称必须与封闭类的名称匹配”。错误。
List<Note> _notes = [
Note(id: '1', date: DateTime.utc(2020, 11, 9), title: 'One', name: 'Name1'),
Note(id: '2', date: DateTime.now(), title: 'Two', name: 'Name2'),
Note(id: '2', date: DateTime.utc(2020, 05, 15), title: 'Two', name: 'Name3')];
_notes.sort((a,b)=>a.date.difference(DateTime.now()).inDays.compareTo(b.date.difference(DateTime.now()).inDays));
答案 0 :(得分:0)
它应该起作用:
_notes.sort((a,b) => a.date.compareTo(b.date));
答案 1 :(得分:0)
您确定已将代码放入例如主要?以下代码可以正常工作:
class Note {
final String id;
final DateTime date;
final String title;
final String name;
Note({this.id, this.date, this.title, this.name});
@override
String toString() => 'Note(id: $id, date: $date, title: $title, name: $name)';
}
void main() {
final _notes = [
Note(id: '1', date: DateTime.utc(2020, 11, 9), title: 'One', name: 'Name1'),
Note(id: '2', date: DateTime.now(), title: 'Two', name: 'Name2'),
Note(id: '2', date: DateTime.utc(2020, 05, 15), title: 'Two', name: 'Name3')
];
_notes.sort((a, b) => a.date
.difference(DateTime.now())
.inDays
.compareTo(b.date.difference(DateTime.now()).inDays));
_notes.forEach(print);
// Note(id: 2, date: 2020-05-15 00:00:00.000Z, title: Two, name: Name3)
// Note(id: 2, date: 2020-10-26 19:55:41.351673, title: Two, name: Name2)
// Note(id: 1, date: 2020-11-09 00:00:00.000Z, title: One, name: Name1)
}