flutter : Checkbox - flutter

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

Related

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,
);

Possible to update/rebuild open DropdownButtonFormField?

Is it possible to uppdate or rebuild open DropdownButtonFormField, e.g. with GetIt or Provider - or perhaps by using a stream - when new data becomes available?
At the moment the options in the dropdown gett added as data arrives while the dropdown is un-expanded, but once the user taps on it only those items that were available at the time they tapped are available and new items are only available if and when the user closes and reopens the dropdown.
My user interface currently has
DropdownButtonFormField(
value: dropdownValue,
icon: const Icon(Icons.keyboard_arrow_down),
items: dropdownItems,
validator: (value) =>
(value == null || value == "0") ? 'Please select a recipient.' : null,
onChanged: (String? newValue) {
if (newValue != null) {
dropdownValue = newValue;
} else {
dropdownValue = "0";
}
setState(() {});
},
)
the dropdownItems comes from
List<DropdownMenuItem<String>>? get dropdownItems {
if (clientListicle.items.isEmpty) {
developer.log('go fetch', name: '_MessagePageState get dropdownItems');
fetchClients();
}
for (var element in clientListicle.items) {
DropdownMenuItem<String> potentialItem =
DropdownMenuItem(value: element.id.toString(), child: Text(element.title));
bool isHave = false;
for (var exstElement in menuItems) {
if (exstElement.value == element.id.toString()) {
isHave = true;
}
}
if (!isHave) {
menuItems.add(potentialItem);
setState(() {});
}
}
if (menuItems.length == 1) {
return null;
} else {
return menuItems;
}
}
clientListicle is a singleton that inherits from a class that extends ChangeNotifier, and is registered in GetIt.
I've had a quick look at the implementation of DropdownButtonFormField and FormField that it extends, thinking maybe one could add functionality to or override a build method or some such, but think maybe I'm missing something simpler/easier and am probably just a bit out of my depth here... :-)
Update, I've tried adding a final _formFieldKey = GlobalKey<FormFieldState>(); key to the dropdown widget thinking I might be able to use that from the getter to trigger a rebuild, but no luck yet.

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

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;
});
}

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);
},
);
}