0
I’m trying to make a Dropdownbutton in my project through a Futurebuilder. Before using Getx to update the Value state, I used setState and it worked. After modifying the project for the use of Getx as a state manager I could not update and is giving error. Follow the codes:
DropdownButton(
icon: Icon(
Icons.arrow_drop_down,
),
iconSize: 20,
hint: Text("Cidade"),
style: TextStyle(
fontFamily: "WorkSansSemiBold",
fontSize: 16.0,
color: Colors.black,
),
items: _listCidades?.map((item){
return new DropdownMenuItem(
child: new Text(item.nome),
value: item.id,
);
})?.toList() ?? [],
onChanged: (value) {
setState(() {
mySelection = value;
});
},
value: mySelection,
),
Code with Getx to manage the state:
FutureBuilder<List<Cidade>>(
future: _loginController.getCity(),
builder: (context, snapshot) {
return DropdownButton(
hint: Text("Cidade"),
items: snapshot.data?.map((cidade) =>
DropdownMenuItem<String>(
child: Text(cidade.nome),
value: cidade.id,
)
)?.toList() ?? [],
onChanged: (newValue){
_loginController.setCidade(RxString(newValue));
mySelection = newValue;
},
value: GetX<LoginController>(
builder: (_)=> _.cidade,
),
);
},
)
Controller code:
var _selectCidade = "0".obs;
get cidade => this._selectCidade.value;
set cidade(value) => this._selectCidade.value = value;