Flutter Switch - onChanged not changing with dynamic value - flutter

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

Related

Change visibility an item on other tabarview not working on setState() Flutter

I have checkbox1 on tabbar 1 and when clicked I set tabbar2's checkbox's visibility to true and step programmatically to tabbar2. However checkbox2 is not appears, just in case I click back on tabbar1 an come back to tabbar2 manually.
I use on tabbar1
onChanged: (bool? value) {
setState(() {
tab1CheckBox = value;
});
//step process to Tabbar2
},
and tabbar2
Visibility(
visible: tab1CheckBox!,
child: Checkbox(
value: tab2CheckBox,
onChanged: (bool? value) {
setState(() {
tab2CheckBox = value;
});
},
))
What should I do something else? The tab1CheckBox = value; is in separet setState() and checkbox2' visibility depends on tab1CheckBox
change this
_tabController.index = 1;
after the setState to this
_tabController.animateTo((_tabController.index + 1) % 2);

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

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

DropdownButton updating with Provider

When i choose item in the below list, it does not change. If i click the others,then nothing happens.
How can i change this value both firestore and UI ?
I know, i need to update value: constantValue, this code, but how i can do that with provider?
Here, button:
Widget dropdownButton(BuildContext context) {
String constantValue = "League Of Legends";
return DropdownButton(
value: constantValue,
onChanged: (newValue) {
context.read<PostProvider>().postCategory = newValue;
},
items: <String>["League Of Legends", "Steam", "Csgo"]
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList());
}
and also provider:
String _postCategory;
String get postCategory => _postCategory;
set postCategory(String value) {
_postCategory = value;
notifyListeners();
}
You need to include something to write FireBase in the code path. Provider doesn't magically do that for you. However, you can have various providers or widgets wake up on that notify_listeners() call.

Flutter DropdownButtonFormField not showing selected item

Using DropdownButtonFormField element to show a lists of cities. It works fine on change event and show the selected item on change event. Issue is that once I try to show selected item from set state its not working. Although state is successfully set and working.
Here is my DropdownButtonFormField code:-
DropdownButtonFormField(
value: _city,
onChanged: (String newValue) {
setState(() {
_city = newValue;
});
},
// initialValue: 'Male',
items: ['Ajman','Al Ain','Dubai','Fujairah','Ras Al Khaimah', 'Sharjah', 'Ajman','Umm Al Quwain']
.map((cityTitle) => DropdownMenuItem(
value: cityTitle, child: Text("$cityTitle")))
.toList(),
)
I set _city in InitState
#override
void initState() {
setFilters();
super.initState();
}
setFilters(){
setState(() {
_city = "Dubai";
});
}
But it's not showing selected value. How can I fix that ?
Meanz when I want to set value of _city from initState its not working