'Is there a way to create dynamic variables in flutter

I am building a flutter application that has multiple radio buttons. I store the list of items listed on the radio buttons in a Firestore collection and fetch them into a list that I then use to organize them into various radio groups.

The data model for the radio items is:

class RadioItems{
  final String itemTitle;
  final String itemBody;
  final String itemId;
  final String valOne;
  final String valTwo;
  final String valThree;
  final String valFour;
  final String valFive;

  RadioItems(
      {required this.itemTitle,
      required this.itemBody,
      required this.itemId,
      required this.valOne,
      required this.valTwo,
      required this.valThree,
      required this.valFour,
      required this.valFive});

I then use PageView.builder for each radio group so that each radio group is presented on its own screen.

PageView.builder(
      physics: const NeverScrollableScrollPhysics(),
      itemCount: radioItems?.length,
      controller: pageController,
      itemBuilder: (context, index) {

      RadioListTile(
            title: Text(radioItems[index]
                    .riskOne),
                     value: 1,
                     groupValue: selection,
                     activeColor: Colors.blue,
                     onChanged: (int? val) {
                     setSelection(val!);
                     setState(() => selectionText =
                     radioItems[index]
                     .valOne);
                            },
                          ),
                    //OTHER RADIO ITEMS ON THIS GROUP ...
     }
)

I have a next and back button on Each PageView. I am currently using a map to store the value of the selected radio when the user clicks on Next, This however poses a challenge when the user clicks the back button as the PageView does not remember the selection.

Is there a way that I can create dynamic variables based on the collection of items on Firestore and use it to store the radio values or a way to remember the user selection when the user goes back?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source