I have a Provider that I was creating with:
ChangeNotifierProvider<WeekList>(create: (context) => WeekList()
But I wanted to initialize the list with adding one week to it at the start. I was pretty stumbed until I read this Stack Overflow article: Flutter/provider- initializing a state with a constructor
But then when I tried it myself with:
ChangeNotifierProvider<WeekList>(create: (context) => WeekList().addWeek(budget: 300.00)),
I got this error:
The return type 'void' isn't a 'WeekList', as required by the closure's context.dartreturn_of_invalid_type_from_closure
Since its a void I tried removing the type like this:
ChangeNotifierProvider(create: (context) => WeekList().addWeek(budget: 300.00)),
But that also did not work.
My WeekList class is:
class WeekList extends ChangeNotifier {
List<Week> listOfWeeks = [];
void addWeek({
required double budget,
}) {
Week newWeek = Week(
budget: budget,
);
listOfWeeks.add(newWeek);
}
}
List<Week> listOfWeeks = [Week()];
or using on constructor body.ChangeNotifierProvider(create: (context) { WeekList weekList = WeekList(); weekList.addWeek(budget: 300.00); return weekList; }) ,