Why is my inline arrow function not changing states as expected? - flutter

So I had a function declared inline and had to remove it because it yielded unexpected results.
RadioListTile<Wealth>(
title: Text("Rich"),
value: Wealth.RICH,
groupValue: state,
onChanged: (newValue) => changeStatus,
)
void changeStatus(Wealth newValue) {
setState(() {
state = newValue;
});
}
That code didn't work, but once I changed it to this it worked..
RadioListTile<Wealth>(
title: Text("Rich"),
value: Wealth.RICH,
groupValue: state,
onChanged: (newValue) {
if (newValue == null) return;
changeStatus(newValue);
},
)
What is the difference between the two and why didn't the first code snippet work? Thanks

The first snippet yielded unexpected results because when you do
onChanged: (newValue) => changeStatus,
you're creating a lambda that accepts a parameter newValue and returns the function changeStatus, without actually calling it.
This should be replaced to
onChanged: (newValue) => changeStatus(newValue),
or
onChanged: changeStatus,
However, if you have null-safety on, this will still raise an error because the function type of onChanged is void Function(Wealth?), while your changeStatus function type is void Function(Wealth). To fix this, just change your changeStatus function to
void changeStatus(Wealth? newValue) {
if (newValue == null) return;
setState(() {
state = newValue;
});
}

Related

flutter : Checkbox

Checkbox(
value: good,
onChanged: (val) {
setState(() {
good = val;
});
}),
There is a red line under the val good = val, what is the reason?
I didn't forget to write above bool good = false;
onChanged provide nullable bool, define as
{required void Function(bool?)? onChanged}
you can provide false on null case like
onChanged: (val) {
setState(() {
good = val??false;
});
}
Find more about null-safety
onChanged: (val) {
setState(() {
good = !good;
});
}
With this you can use switch good value from true to false or false to true every time you trigger this function

How does the onPressed voidcallback change the boolean in my function?

Situation:
I have a checkbox in one place and i am sending the callback etc. up the widget tree to run a setState and run the function applyFilters().
The NeededChecked is also routed up to the checkbox-value.
Question:
What i am struggling to understand is why this works.
Specifically how the onPressed callback is able to set the value of the bool isNeededState to true/false?
Here is the code that is run. The only important part is the passing of the bool isNeededState to the neededCheked.
void neededFilterCalled(bool isNeededState) {
setState(() {
NeededChecked = isNeededState;
applyFilters();
});
}
And here is the checkbox widget:
Widget build(BuildContext context) {
return Checkbox(
value: isNeededChecked,
onChanged: neededFilterCalled,
);
}
Writing
onChanged: neededFilterCalled,
is shorthand for
onChanged: (value) => neededFilterCalled(value),
onChanged provide nullable bool, defined as
required void Function(bool?)? onChanged
You can accept null value and provide false on null case like
void neededFilterCalled(bool? isNeededState) {
setState(() {
isNeededChecked = isNeededState ?? false;
applyFilters();
});
}
return Checkbox(
value: isNeededChecked,
onChanged: neededFilterCalled,
);

How do I create an anonymous function that doesn't generate a void function problem in Flutter

How do I create an inline function to fix this error:
The argument type 'void Function()' can't be assigned to the parameter type 'void Function.
Consider the following code:
Switch(value: _showChart, onChanged: () { //this generates the error
setState(() {
_showChart = value;
});
},)
If I extracted it to a function it would look something like this, but I don't want to do that:
VoidCallback? onSwitch(val){
setState(() {
_showChart = val;
});
}
Is there a way to do that inline?
onChanged: contains a value parameter.
You are missing a typo, the value parameter. It will be,
Switch(
value: true,
onChanged: (value) {
setState(() {
_showChart = value;
});
},
),

No rebuild after choosing value in Dropdown

I am sending the choosen value of the DropdownMenu via Callback to the parent widget (see code below).
return DropdownButton<String>(
hint: Text(widget.hint),
value: valueChoose,
items: widget.users.map((dynamic valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
besetzungsList.add(valueChoose);
widget.besetzungsListChanged(valueChoose);
widget.fromDropDown(true);
});
},
);
The problem is with "besetzungsList[i] = value;" no rebuild occurs. But I need the choosen value to be shown in the UI. If I set the value via the insert function then it works. But I need to replace the value inside the list.
DataCell(DropDown(
hint: "Wählen",
users: users,
besetzungsListChanged: (String value) {
besetzungsList[i] = value;
},
fromDropDown: (bool value) => fromDropDown = value,
))
Is the parent widget a stateful widget?
you can try to call setState in benutzungsListChangedto enforce rebuild of the parent widget
DataCell(DropDown(
hint: "Wählen",
users: users,
besetzungsListChanged: (String value) {
setState(() {besetzungsList[i] = value;});
},
fromDropDown: (bool value) => fromDropDown = value,
))

Flutter Switch - onChanged not changing with dynamic value

I am getting rows of data in JSON using post request, while changing the status of the table row my checkbox value is not changing.
rows: _Clist.map(
((clist) => DataRow(cells: [
DataCell(
StatusSwitch(clist.status),
),
])),
).toList(),
//////////////////////////////////////////////////////
StatusSwitch(SState){ // SState is dynamic value, getting from json post data
bool SStatus = SState == 1 ? true : false;
return CupertinoSwitch(
activeColor: Color(0xFF2ECC71),
trackColor: Color(0xFFEFEFEF),
value: SStatus,
onChanged: (bool newValue) {
setState(() {
SStatus = newValue;
});
print(SStatus);
},
);
}
You should move SStatus to class visibility level. Now it's a local variable and calculated each time based on SState which seems not changed.
2 options:
You should change SState instead of SStatus when new value comes.
Move SStatus out of method. And declare as class property.
bool SStatus = SState == 1 ? true : false;
StatusSwitch(SState){ // SState is dynamic value, getting from json post data as a parameter
return CupertinoSwitch(
activeColor: Color(0xFF2ECC71),
trackColor: Color(0xFFEFEFEF),
value: SStatus,
onChanged: (bool newValue) {
setState(() {
SStatus = newValue;
});
print(SStatus);
},
);
}