No rebuild after choosing value in Dropdown - flutter

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

Related

How to access dropdownlist selected value in flutter and use it in another widget

This is my drop down list code
String dropdownValue = "a";
#override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>[
'a','b','c'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}
I want to use the selected value here
uploadDatatoFirebase() async {
*firebase connection code*
await FirebaseFirestore.instance
.collection(**selected value in drop down**) // the value from the box goes here
}
I can't seem to find a solution please help i want to create a database depending on the select box value any other techniques will also be welcomed
https://pub.dev/packages/get_it This is the package I use so I can can access a single instance of a class anywhere in the app, it is very popular and very well maintained. You could do this
setState(() {
dropdownValue = newValue!;
GetIt.I.get<ControllerWhatever>().selectedValueInDropDown = newValue!;
})
Just register the 'global controller instance' just as described in the get_it page and you will be able to get the value from any attribute you want anywhere in your app. It will make you life a lot easier

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

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

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