1

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);
    
  }
}
5
  • How about doing inside notifier List<Week> listOfWeeks = [Week()]; or using on constructor body. Mar 22, 2022 at 16:35
  • @YeasinSheikh sorry, not exactly sure what you mean by that suggestion. Mar 22, 2022 at 16:37
  • 1
    Please try this ChangeNotifierProvider(create: (context) { WeekList weekList = WeekList(); weekList.addWeek(budget: 300.00); return weekList; }) ,
    – Jimenez
    Mar 22, 2022 at 16:44
  • @Mardel I had to edit some formatting as the code was missing a ) but it worked! Thanks so much! Care to explain what I was doing wrong and how your code works? Mar 22, 2022 at 16:56
  • 1
    We need to return WeekList object, but in your previous code, you didn't return anything. Otherwise, you call addWeek function and it doesn't mean to return WeekList object. That's all. Happy coding
    – Jimenez
    Mar 22, 2022 at 17:04

1 Answer 1

1

In create you need to return a WeekList. The function addWeek has a type of void not WeekList. You can change that function to return WeekList that will then create the ChangeNotifierProvider with the listOfWeeks containing the newWeek because now the listOfWeeks will be returned.

class WeekList extends ChangeNotifier {
  List<Week> listOfWeeks = [];

  WeekList createWeekList({
    required double budget,
  }) {
    Week newWeek = Week(
      budget: budget,
    );
    listOfWeeks.add(newWeek);
    return listOfWeeks; 
  }
}

An other option is to initialise the listOfWeeks with a Week and then not use addWeek in create

ChangeNotifierProvider<WeekList>(create: (context) => WeekList())

...

class WeekList extends ChangeNotifier {
  List<Week> listOfWeeks = [Week(budget: budget: 300.00)];

  void addWeek({
    required double budget,
  }) {
    Week newWeek = Week(
      budget: budget,
    );
    listOfWeeks.add(newWeek);
    
  }
}

And another option if you need specify the budget amount would be to initials WeekList with a budget amount.

ChangeNotifierProvider<WeekList>(create: (context) => WeekList(budget: 300.00))

...

class WeekList extends ChangeNotifier {
  final double budget;
  const WeekList({required this.budget}){
    listOfWeeks = [Week(budget: budget: budget)]
  }
  Late final List<Week> listOfWeeks;

  void addWeek({
    required double budget,
  }) {
    Week newWeek = Week(
      budget: budget,
    );
    listOfWeeks.add(newWeek);
    
  }
}

Then there is one more option i can think of and that is to use the cascade notation. https://dart.dev/guides/language/language-tour#cascade-notation Which would look like this.

 ChangeNotifierProvider<WeekList>(create: (context) => WeekList()..addWeek(budget: 300.00)),
5
  • but then that code really isnt a"AddWeek" it just returns and doesn't do the add function. Mar 22, 2022 at 16:57
  • 1
    It does add the week, but it also returns the updated list maybe changing the name of the function would be better. Updated the answer.
    – Shannon
    Mar 22, 2022 at 17:00
  • Gotcha, I see it now! Still changes the functionality a bit for that function though. Mar 22, 2022 at 17:01
  • 1
    Updated answer with different options.
    – Shannon
    Mar 22, 2022 at 17:17
  • Awesome will try this out in a bit and let you know! Mar 22, 2022 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.