Flutter DropdownButtonFormField not showing selected item - flutter

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

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

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

How can I solve "There should be exactly one item with [DropdownButton]'s value: null." error in Flutter?

I want to get some details from database and put that values to dropdown menu. I have successfully got the information that I want to put on dropdown from database and put it into dropdown list.
But when I tried to select one item from dropdown list, it shows this error.
There should be exactly one item with [DropdownButton]'s value: null.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
Here is the code I wrote so far.
Variables
var supplier;
var _supplierDetails = [];
Init Method
#override
void initState() {
super.initState();
getSupliers();
}
Get Suppliers
void getSupliers() async {
final responseSup =
await http.get(Uri.parse('http://170.14.0.0:8020/supplier/'));
final jsonDataSup = jsonDecode(responseSup.body) as List;
print(jsonDataSup);
setState(() {
_supplierDetails = jsonDataSup;
});
}
DropDown menu implemented
DropdownButton(
hint: Text('Select Supplier'),
items: _supplierDetails.map((list) {
return DropdownMenuItem(
child: Text(list['Name']),
value: list['id'].toString(),
);
}).toList(),
onChanged: (value) {
setState(() {
supplier = value;
});
},
value: supplier,
)
Can someone help me to fix this issue please?
Thank you
It seems there are more than one item where item['id'] is null. DropdownButton need all it's items to have a unique 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,
))

How to update Inner Widget in Flutter

Hi I am having an issue with radio and checkbox on flutter. I have created my own widget on the purpose of having different return widget depending on what is needed as on the picture bellow since I selected change pin the return will be a checkbox but the return widget is a checkbox while the save button is stagnant or didn't change but I am having an issue when i used radio or check box even Visibility. When I try to update the setState() it is not updating or reloading the widget. The code below is just the first return of the widget which will be a check box. When i try to unchecked the button the value becomes falls but the UI is not unchecked.
sample Image
Widget getInputType(val){
if(val == 'Pin Lock'){
return CheckboxListTile(
title: Text('Pin Login'),
value: isSwitchPin,
onChanged: (value){
setState(() {
print(value);
this.isSwitchPin = value;
});
});
}
}
You can try:
1, Define a field
ValueChanged<bool> onEnventChange;
2, On initState()
onEnventChange = (value){
setState(() {
print(value);
this.isSwitchPin = value;
});
});
3, Define your function
Widget getInputType(val){
if(val == 'Pin Lock'){
return CheckboxListTile(
title: Text('Pin Login'),
value: isSwitchPin,
onChanged: onEnventChange);
}
}