I am still new to flutter and learning how to deal with lists. In this case, I have a list of millisecondsSinceEpoch
so (List<int>
in Firestore) and I want to generate a new List<DateTime>
of dates from those integers. However, I am not sure how to handle that with the DateTime.fromMillisecondsSinceEpoch()
.
The error is The argument type 'List<int>' can't be assigned to the parameter type 'int'.
I understand it is not looking for a list, but not sure how to pass that properly.
//updatesDateEpoch is a list of int
List<int> datesSec = logRecord.updatesDateEpoch.asList();
//error caused when passing dateSec
List<DateTime> datesList = [DateTime.fromMillisecondsSinceEpoch(datesSec)];
2
Answers
You are getting this error because the
DateTime.fromMillisecondsSinceEpoch
function is expecting a int parameter and you are passing the whole List, to simplify your code and solve the error you can do this:Code:
List.generate
takes two parameters:datesSec.length
as the first parameter.datesSec[index]
and then we pass the element inDateTime.fromMillisecondsSinceEpoch(datesSec[index])
.And so a new millisecond list is successfully created.
Aside: some people are working too hard at this. Whenever you have the form:
you should be getting a lint that says "unnecessary closure", because you can replace that with just:
because the function name is indeed a function that takes a single value and returns a result!