Reputation: 1
in my code i have a list of RadioListTile returned from StreamBuilder, being displayed correctly but not checked when clicking on an option.
StreamBuilder<List<Endereco>>(
stream: noteStream(uid),
builder: (context,snapshot){
if(!snapshot.hasData){
return Center(child: CircularProgressIndicator(),);
} else if(snapshot.hasError){
return Center( child: Text("An Error Occured"));
}
else if(snapshot.hasData){
return Column(
children: [
Column(
children: snapshot.data!.map((endereco){
// int index = snapshot.data!.indexOf(endereco);
return RadioListTile<Endereco>(
value: endereco,
groupValue: selectedEndereco,
title: Text(endereco.rua + ", nº " + endereco.numero.toString() +", "+ endereco.complemento),
subtitle: Text(endereco.bairro + " - "+ endereco.cidade + "-"+ endereco.estado + " "+ endereco.cep),
onChanged: (currentEndereco) {
setState((){
selectedEndereco = currentEndereco;
});
print("Current $selectedEndereco");
},
activeColor: kPrimaryColor,
selected: selectedEndereco == endereco,
);
}).toList()
),
Stream<List<Address>> noteStream(String uid){
try{ return _db.collection("users").doc(uid).collection("address").snapshots().map((address){ final List addressFromFirestore = []; for(final DocumentSnapshot<Map<String,dynamic>> doc in address.docs){ addressFromFirestore.add(Address.fromDocumentSnapshot(doc: doc)); } return addressFromFirestore; }); }catch(e){ rethrow; } }
Upvotes: 0
Views: 257
Reputation: 52
It depends on where you initialized your selectedEndereco and how you did it. because if you for example initialized it in the beginning with a value then you call setState it will reset the initialized value at the beginning again
class _MyHomePageState extends State<MyHomePage> {
List<Endereco> enderecos = [
Endereco(name: 'Endereco1', id: 1),
Endereco(name: 'Endereco1', id: 2)
];
late Endereco selectedEndereco;
@override
void initState() {
super.initState();
selectedEndereco = Endereco(name: 'Endereco1', id: 2);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: enderecos
.map((e) => RadioListTile<Endereco>(
value: e,
groupValue: selectedEndereco,
title: Text(e.name),
onChanged: (currentEndereco) {
setState(() {
selectedEndereco = currentEndereco!;
});
print("Current $selectedEndereco");
},
activeColor: Colors.red,
selected: selectedEndereco == e,
))
.toList(),
),
),
);
}
}
Upvotes: 0