I am making a flutter app using provider pattern. And I am getting null error when I launch my app.
button_data.dart goes like this.
class ButtonData extends ChangeNotifier {
List<Button> _buttons = [
Button(
type: "A",
numberone: "1",
numbertwo: "2",
numberthree: "3",
),
Button(
key: "B",
numberone: "A",
numbertwo: "B",
numberthree: "C",
),
];
Button _selectedButton;
List<Button> get buttons => _buttons;
Button get selectedButton => _selectedButton;
void setSelectedItem(Button s) {
_selectedButton = s;
notifyListeners();
}
Button getType(String value) {
return _buttons
.where((button) => button.type == value).first;
} // it is no use...
}
And I want those button displayed diffently when I switched a type.
new 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)),
])
But Error seems to get returned since maybe I don't set default value. it says 'the getter 'type' was called on null' If possible I want 'type A' to be set as default value so that display shows 1, 2, 3 on the buttons.
So, to figure out null error,
I tried the initState
function below, but that throws more errors.
1. There isn't a setter named 'selectedButton' in class 'ButtonData'
2. The expression here has a type of void and therefore can't be used
@override
void initState() {
Provider.of<ButtonData>(context).selectedButton = Provider.of<ButtonData>(context).setSelectedItem(Provider.of<ButtonData>(context).buttons.first);
super.initState();
}
I've been trying to find similar questions but I can't find the exact questions so I ask you guys.