'how to keep data for dropdownitem with getx

I have a dropdownbutton, and it is fine to select a dropdownItem and pass to other widget through GetXController, what I want is user still can come back to dropdownButton page and the dropdownItem value is saved still..But currently what I met is the value will come back to default(so the value before I selected) or thrown the following error:

There should be exactly one item with [DropdownButton]'s value: Instance of 'DropdownItem'. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:dropdown_button2/dropdown_button2.dart':
Failed assertion: line 974 pos 11: 'items == null ||
              items.isEmpty ||
              value == null ||
              items.where((DropdownMenuItem<T> item) {
                    return item.value == value;
                  }).length ==
                  1'

Here is my code:

 Column(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 64,
              child: GetBuilder<ProfileRegisterController>(
                builder: (profileRegisterController) {
                  return DropdownButton2(
                    itemHeight: 48,
                    dropdownElevation: 0,
                    iconEnabledColor: AppColors.hintFontColor,
                    dropdownDecoration: const BoxDecoration(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(0),
                        topRight: Radius.circular(0),
                        bottomLeft: Radius.circular(16),
                        bottomRight: Radius.circular(16),
                      ),
                    ),
                    style: mediumTextBoldStyle(textColor: AppColors.mainFontColor),
                    value: profileRegisterController.currentSelectedItem ??
                        genderItems[2],
                    focusNode: _genderFocusNode,
                    focusColor: Colors.transparent,
                    offset: const Offset(0, 5),
                    hint: Text(
                      'Gender',
                      style: hintTextStyle(textColor: AppColors.hintFontColor),
                    ),
                    underline: Container(
                      height: 1,
                      color: AppColors.hintFontColor,
                    ),
                    isExpanded: true,
                    items: genderItems.map((DropdownItem value) {
                      return DropdownMenuItem(
                          value: value,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              SvgPicture.asset(
                                value.genderPath,
                                color: AppColors.mainFontColor,
                              ),
                              smallSpacing(),
                              Text(
                                value.gender,
                                style: mediumTextBoldStyle(
                                    textColor: AppColors.mainFontColor),
                              ),
                            ],
                          ));
                    }).toList(),
                    onChanged: (value) {
                      setState(
                        () {
                          profileRegisterController.setGender(value);
                        },
                      );
                      profileRegisterController.validateButton();
                    },
                  );
                },
              ),
            ),
            Text(
              'Gender can not be changed after confirmation',
              style: smallTextStyle(textColor: AppColors.hintFontColor),
            ),
          ],
        );

For the controller part is quit simple:

class ProfileRegisterController extends GetxController {

  DropdownItem currentSelectedItem;


  void setGender(value){
    currentSelectedItem = value;
    update();
  }
}

So thanks a lot for any help!



Sources

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

Source: Stack Overflow

Solution Source