getter was called on null when using provider in flutter getter was called on null when using provider in flutter dart dart

getter was called on null when using provider in flutter


There are a lot of things you are doing wrong here. You need to understand that Provider can expose to all the Widgets below it. Additionally, this will also help others understand Flutter and Provider a bit more.

First of all, make sure that you are exposing the ButtonData object from somewhere above the Row where you want to use the value.

Like so:

//You can use this in the build method, and wrap it on top of your page Scaffold. //@override//Widget build(BuildContext context) {return ChangeNotifierProvider(        create: (_) => ButtonData(),        child: Scaffold(          appBar: _appBar(),          body: _body(),        ),      );//}


Second, the way you are using to get buttons in the Row widget is very bad, when you want to use your object multiple times like in your code, you should use the Consumer widget to expose your ButtonData as a variable and use that instead of the whole line: Provider.of<ButtonData>(context).

Check this out:

//WRONG WAYnew Row(children: [  buildButton(Provider.of<ButtonData>(context).getNumberOne(Provider.of<ButtonData>(context).selectedButton.type)),  buildButton(Provider.of<ButtonData>(context).getNumberTwo(Provider.of<ButtonData>(context).selectedButton.type)),  buildButton(Provider.of<ButtonData>(context).getNumberThree(Provider.of<ButtonData>(context).selectedButton.type)),])//CORRECT WAYConsumer<ButtonData>(  builder: (BuildContext context, ButtonData buttonData, Widget child) => Row(    children: <Widget>[      buildButton(buttonData.getNumberOne(buttonData.selectedButton.type)),      buildButton(buttonData.getNumberTwo(buttonData.selectedButton.type)),      buildButton(buttonData.getNumberThree(buttonData.selectedButton.type)),    ],  ),);

Also if you are fetching a Provider value in your initState function, that means you are fetching your value OUTSIDE of the widget tree. (Whatever you return in your build function is your Widget Tree). To fetch a value outside of the widget tree, use this.

///WRONGProvider.of<ButtonData>(context).selectedButton = Provider.of<ButtonData>(context).setSelectedItem(Provider.of<ButtonData>(context).buttons.first);///CORRECT///You need to set `listen` to false.Provider.of<ButtonData>(context, listen: false).selectedButton = Provider.of<ButtonData>(context, listen: false).setSelectedItem(Provider.of<ButtonData>(context, listen: false).buttons.first);///BETTERButtonData buttonData = Provider.of<ButtonData>(context, listen: false);buttonData.selectedButton = buttonData.setSelectedItem(buttonData.buttons.first);

One more tip...You don't need to use new keyword, if you have seen this in tutorials, then that is because those tutorials are very old, and are using Dart 1, currently in Dart 2, the new keyword is no longer required.

Hope this helps!!

UPDATE:

You also have missing declarations in your ButtonData class.

Explanations for the errors you mentioned in the comment:

//If you want to use the following:Provider.of<ButtonData>(context, listen: false).selectedButton = xyz;//You need to declare a setter named `selectedButton` in your ButtonData// class. Like so:set selectedButton(Button button) {    _selectedButton = button;    notifyListeners();}//Currently, in your ButtonData class, you have not declared a setter,// instead you created a method there for updating the value. //This one.void setSelectedItem(Button s) {    _selectedButton = s;    notifyListeners();}//If you want to use this method for updating your `selectedButton` value,// you can use the following line of code.Provider.of<ButtonData>(context, listen: false).setSelectedItem(xyz);


For most cases you can fix it just using the Statefull.mounted property.

initState() {  if (mounted) {    context.read<YouType>();  }  super.initState();}