I have a simple class named Person, having three fields name, country and birthdate.
here I want to make a getter that returns a list of persons whose birthday is coming to this week.
and one more thing that I am getting error:
Person class contains a field named birthday which type is DateTime
How to assign value to DateTime
?. I am doing as following which showing an error with birthday field
Person(name: 'Alex',country: 'Zambia',birthdate: '12-09-1990'),
class Person
{
String name;
String country;
DateTime birthdate;
Person({required this.name,required this.country,required this.birthdate});
}
and here is the code where I will use this getter for displaying user list
class myscreen extends StatelessWidget {
myscreen({Key? key}) : super(key: key);
List<Person> personlist=[
Person(name: 'Alex',country: 'Zambia',birthdate: '12-09-1990'),
Person(name: 'Suzi',country: 'Laos',birthdate: '14-08-1980'),
Person(name: 'Lyli', country: 'Nepal', birthdate: '27-09-2002'),
];
List<Person> get person_birthday_thisweek{
List<Person> templist=[];
//what to code to get the list of persons whose birthday is in this week
return templist;
}
CodePudding user response:
try this.. and use this package for DateTime format
CodePudding user response:
i update it for month end date in dart you can parse string to datetime DateTime.parse('1900-10-08 00:00:00.000')
List<Person> get person_birthday_thisweek{
List<Person> personlist = [
Person(name: 'PPP',country: 'Zambia',birthdate: DateTime.parse('1900-10-08 00:00:00.000')),
Person(name: 'LLL',country: 'Zambia',birthdate: DateTime.parse('1900-09-29 00:00:00.000')),
Person(name: 'Alex',country: 'Zambia',birthdate: DateTime.parse('1990-10-03 00:00:00.000')),
Person(name: 'Suzi',country: 'Laos',birthdate: DateTime.parse('1980-08-14 00:00:00.000')),
Person(name: 'Lyli', country: 'Nepal', birthdate: DateTime.parse('2002-09-27 00:00:00.000')),
];
List<Person> templist=[];
var now = DateTime.now();
//now = DateTime.parse("2022-10-01 13:13:40.305728");
print(now);
var start = now.day - now.weekday;
var lst = List.generate(7, (index) => DateTime(now.year, now.month, start index));
for (var p in personlist) {
var fakeDate = DateTime(now.year, p.birthdate.month, p.birthdate.day);
if (lst.contains(fakeDate)) {
print(p.name);
templist.add(p);
}
}
return templist;
}
CodePudding user response:
For you first issue you can convert your string to DateTime by using intl package, like this:
DateTime tempDate = new DateFormat("dd-MM-yyyy")
.parse('12-09-1990');
but let assume your list is like this:
List<Person> personlist = [
Person(name: 'Alex', country: 'Zambia', birthdate: DateTime(1990, 09, 12)),
Person(name: 'Suzi', country: 'Laos', birthdate: DateTime(1980, 08, 14)),
Person(name: 'Lyli', country: 'Nepal', birthdate: DateTime(2002, 09, 27)),
Person(name: 'Lysadli', country: 'Nepal', birthdate: DateTime(2022, 10, 03))
];
so change your get method to this:
List<Person> get person_birthday_thisweek {
final date = DateTime.now();
DateTime startDate =
getDate(date.subtract(Duration(days: date.weekday - 1)));
DateTime endDate =
getDate(date.add(Duration(days: DateTime.daysPerWeek - date.weekday)));
List<Person> templist = personlist
.where((element) =>
(startDate.isBefore(element.birthdate) ||
startDate == element.birthdate) &&
endDate.isAfter(element.birthdate))
.toList();
return templist;
}
the result would be:
for (var element in person_birthday_thisweek) {
print("object = ${element.birthdate}");
}
//2022-10-03 00:00:00.000