radio button not working in streambuilder

151 Views Asked by At

I run radio button widgets in Streambuilder, but the selected option does not become active when the selection is made, why? I am facing this problem when I want to select by String value. What part am I doing wrong in this case? Ra Could you help?

StreamBuilder<QuerySnapshot>(
              stream:...
              builder: (context, snapshot) {
             ....
                    return buildBody(context, snapshot.data.docs);
                }
              },
            )      

Widget buildBody(BuildContext context, List<DocumentSnapshot> snapshot) {
        return ListView(
            padding: EdgeInsets.only(top: 20),
            children:
                snapshot.map<Widget>((e) => buildListItem(context, e)).toList());
      }

  Widget buildListItem(BuildContext context, DocumentSnapshot data) {
    final row = Survey.fromSnapshot(data);
    String _value = '';

    return Padding(
        padding: EdgeInsets.all(10),
        key: ValueKey(row.name),
        child: Container(
          decoration:.....
          child: ListTile(
            title: .....
            leading: Radio<String>(
              activeColor: Colors.blueGrey,
              groupValue: _value,
              value: row.name,
              onChanged: (value) {
                setState(() {
                  _value = value;
                });
              },
            ),
            onTap: () {
              FirebaseFirestore.instance.runTransaction((transaction) async {
                final freshSnapShot = await transaction.get(row.reference);
                final fresh = Survey.fromSnapshot(freshSnapShot);

                await transaction.update(
                    row.reference, {'isim': row.name, 'oy': row.vote + 1});
              });
            },
          ),
        ));
  }
}
2

There are 2 best solutions below

1
On

Declare your String _value = ''; outside of your build function, as of right now every time you setState your _value will be set to an empty string instead of the value you want.

3
On

I think there are differences in the type of variables. _value is a string but onChange: (value) {} return you type boolean.. so kindly look into it and if helpful upvote :')