How to get Radio to work on flutter when using dynamic object?

1.1k Views Asked by At

I'm new to flutter and I have issues with Radio. I got it to work perfectly when using the "regular" method:

int _groupValue=-1;
...
...
child: Align(
                alignment: Alignment.center,
                child: Column(
                  children: <Widget>[
                    RadioListTile(activeColor: Colors.black,
                      groupValue: _groupValue,
                      value: 1,
                      onChanged: (value) { setState(() {
                        _groupValue=value;
                      });  },
                      title: Text("a"),
                    ),
                    RadioListTile(activeColor: Colors.black,
                      groupValue: _groupValue,
                      value: 2,
                      onChanged: (value) { setState(() {
                        _groupValue=value;
                      });  },

                      title: Text("b"),
                    ),
                  ],
                ),
              ),

Since I'm using data from API to create the radio buttons I've changed this code to this:

child: Align(
                    alignment: Alignment.center,
                    child:            children: radioListView
                    ),
                  ),

and on click of a button i call async method to download the data from the API and like this :

void getApiData() async {
    ...
    ...
    ...
    setState() { 
        var radioListView = List<RadioListTile>();
        for (Map<String, dynamic> c in apidata)) {
          radioListView.add(new RadioListTile(
            groupValue: _groupValue,
              value: c['option_value'],
            title: c['title'],
            onChanged: (value) { setState(() {
                        _groupValue=value;
                      });
            ),

          ));
      }
    }
} 

using the first code it works but using the second code I just get to see the items but nothing happens when I click on the radio buttons (although the onchanged does trigger because I tried to print the value and it's fine) what am I doing wrong?

1

There are 1 best solutions below

0
On

I found the solution here: https://www.didierboelens.com/2018/05/hint-5-how-to-refresh-the-content-of-a-dialog-via-setstate/

The problem was that I had to seperate the widgets and create another stateful widget for the radio