Flutter中值的初始化问题

问题描述 投票:0回答:3

我试图在下拉菜单中选择一个值,并在下拉菜单关闭时将该值传输到下拉菜单标题,但是当我尝试这样做时出现错误消息,但该值确实出现在下拉菜单标题中:

未处理的异常:处置后使用了 S2SingleSelection。 一旦你在 S2SingleSelection 上调用了 dispose(),它就不能再使用了。

我在任何时候都不明白我使用 S2SingleSelection.

使用的包:https://pub.dev/packages/awesome_select版本6.0.0

这是我的代码和初始值:


String? _selectedLevel;

final firestore = FirebaseFirestore.instance;

SmartSelect.single(
                                selectedValue: _selectedLevel,
                                onChange: (newValue) {
                                  setState(() {
                                    _selectedLevel = newValue.value;
                                  });
                                },
                                choiceItems: documents.map((DocumentSnapshot<dynamic> dataDoc) {
                                  return S2Choice(
                                    value: dataDoc.data()['field1'],
                                    title: dataDoc.data()['field1'],
                                  );
                                }).toList(),
                                placeholder: 'N/A',
                                tileBuilder: (context, value) {
                                  return Container(
                                    padding: EdgeInsets.symmetric(
                                      vertical: MediaQuery.of(context).size.height * 0.01,
                                      horizontal: MediaQuery.of(context).size.width * 0.03,
                                    ),
                                    decoration: BoxDecoration(
                                      color: Theme.of(context).colorScheme.onPrimary,
                                      borderRadius: BorderRadius.circular(20),
                                    ),
                                    child: Row(
                                      children: [
                                        Padding(
                                          padding: EdgeInsets.symmetric(
                                            horizontal: MediaQuery.of(context).size.width * 0.035,
                                          ),
                                          child: Icon(
                                            FontAwesomeIcons.locationDot,
                                            color: Theme.of(context).colorScheme.primary,
                                            size: MediaQuery.of(context).size.width * 0.065,
                                          ),
                                        ),
                                        Expanded(
                                          child: S2Tile(
                                            value: Text(
                                              _selectedLevel != null ? "" : "",
                                              style: TextStyle(
                                                color: Theme.of(context).colorScheme.primary,
                                                fontSize: MediaQuery.of(context).size.width * 0.055,
                                              ),
                                            ),
                                            onTap: () {
                                              value.showModal();
                                            },
                                            title: Text(
                                              _selectedLevel != null ? _selectedLevel! : "Choose",
                                              style: TextStyle(
                                                fontSize: MediaQuery.of(context).size.width * 0.055,
                                                color: Theme.of(context).colorScheme.primary,
                                                overflow: TextOverflow.ellipsis,
                                              ),
                                            ),
                                            loadingText: "",
                                            trailing: Icon(
                                              FontAwesomeIcons.angleDown,
                                              color: Theme.of(context).colorScheme.primary,
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  );
                                },
                                modalConfig: const S2ModalConfig(
                                  title: '',
                                  type: S2ModalType.bottomSheet,
                                ),
                                choiceConfig: S2ChoiceConfig(
                                  type: S2ChoiceType.radios,
                                  style: S2ChoiceStyle(
                                    showCheckmark: true,
                                    color: Theme.of(context).colorScheme.primary,
                                    titleStyle: TextStyle(
                                      fontSize: MediaQuery.of(context).size.width * 0.055,
                                      color: Theme.of(context).colorScheme.primary,
                                    ),
                                  ),
                                ),
                                choiceBuilder: (context, item, isSelected) {
                                  return Padding(
                                    padding: EdgeInsets.symmetric(
                                      vertical: MediaQuery.of(context).size.height * 0.015,
                                      horizontal: MediaQuery.of(context).size.width * 0.03,
                                    ),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        GestureDetector(
                                          onTap: () {
                                            setState(() {
                                              _selectedLevel = isSelected.value;
                                            });
                                            if (_selectedLevel != null) {
                                              setState(() {
                                                item.onModalClose(true);
                                                item.closeModal();
                                              });
                                            }
                                          },
                                          child: Row(
                                            children: [
                                              Icon(
                                                _selectedLevel == isSelected.value ? FontAwesomeIcons.solidCircleCheck : FontAwesomeIcons.circle,
                                                color: Theme.of(context).colorScheme.primary,
                                                size: MediaQuery.of(context).size.width * 0.055,
                                              ),
                                              SizedBox(
                                                width: MediaQuery.of(context).size.width * 0.05,
                                              ),
                                              Text(
                                                isSelected.value,
                                                style: TextStyle(
                                                  overflow: TextOverflow.ellipsis,
                                                  fontSize: MediaQuery.of(context).size.width * 0.055,
                                                  color: Theme.of(context).colorScheme.primary,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        Row(
                                          children: [
                                            GestureDetector(
                                              onTap: () {
                                                // TODO EDIT NAME SITE
                                              },
                                              child: Icon(
                                                FontAwesomeIcons.penToSquare,
                                                color: Theme.of(context).colorScheme.primary,
                                                size: MediaQuery.of(context).size.width * 0.055,
                                              ),
                                            ),
                                            SizedBox(
                                              width: MediaQuery.of(context).size.width * 0.05,
                                            ),
                                            GestureDetector(
                                              onTap: () {
                                                setState(() {
                                                  isSelected.value;
                                                });
                                                if (isSelected.value != null) {
                                                  CRUDMethodFirestore(
                                                    selectedLevel: isSelected.value,
                                                    itemModal: item,
                                                    context: context,
                                                  ).delete();
                                                }
                                                setState(() {
                                                  _selectedLevel = null;
                                                });
                                              },
                                              child: Icon(
                                                FontAwesomeIcons.trash,
                                                color: Theme.of(context).colorScheme.primary,
                                                size: MediaQuery.of(context).size.width * 0.055,
                                              ),
                                            ),
                                          ],
                                        )
                                      ],
                                    ),
                                  );
                                },
                              ),

问题出现在我的两个 GestureDetector 执行期间,在 onTap 级别,下拉菜单(模态)关闭并在我的初始值变量 _selectedLevel 中定义了新值,它可以正常传输值,但错误消息出现在控制台,但不影响我的代码的目标。

如果你能帮我解决我的错误,那就太好了,它告诉我 S2SingleSelection 在被处置后被使用,除非在任何情况下我都为我处置它。

谢谢。

flutter dart package dispose pub.dev
3个回答
0
投票

做 2 个更正,一个是 onChange 添加 newValue.value.toString() 另一个是不要在 onChange 方法中使用设置状态。而不是使用 ValueNotifier

//Initialise variable
 ValueNotifier<String> _selectedLevel = ValueNotifier<String>(//YourInitialValue else empty string);  

 onChange: (newValue) {

       _selectedLevel.value = newValue.value.toString();

  },

0
投票

错误不断弹出,我的代码声称在处理后使用了 S2SingleSelection。

问题出在这段代码:

旧代码:

onTap: () {                         
  setState(() {
    _selectedLevel = isSelected.value;
  });
  if (_selectedLevel != null) {
     setState(() {
       item.onModalClose(true);
       item.closeModal();
     });
  }
},

带有 ValueNotifier 的新代码:

onTap: () {                         
  _selectedLevel.value = isSelected.value;
  if (_selectedLevel != "") {
     setState(() {
       item.onModalClose(true);
       item.closeModal();
     });
  }
},

问题出在我的方法:item.onModalClose(true) ; 该方法有效,模式关闭并初始化我的值并将其显示在屏幕上,但它会生成我的 dispose() 错误。如果我删除这个并且只留下方法 item.closeModal(),这个会得到我的值,关闭模态但不初始化要在屏幕上显示的值,它保持在旧值或预定义值,如果没有选择了其他值。


0
投票

晚安!我也有同样的问题!有人可以帮助我吗?

© www.soinside.com 2019 - 2024. All rights reserved.