首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在小部件树中检测到重复的GlobalKey,每个子节点必须精确布局一次。

在小部件树中检测到重复的GlobalKey,每个子节点必须精确布局一次。
EN

Stack Overflow用户
提问于 2021-03-25 06:14:42
回答 1查看 519关注 0票数 1

enter code herei有一个抽屉类,它有包含服务列表的多页,在每个页面中都添加了带有 GlobalKey<FormState> _formKey = GlobalKey<FormState>();的服务页。

我的页面流就像抽屉->实用程序列表页面->添加实用程序

,但我收到了错误:

The following assertion was thrown building Form-[LabeledGlobalKey<FormState>#98e90](state: FormState#bd67c): 'package:flutter/src/widgets/will_pop_scope.dart': Failed assertion: line 135 pos 12: '_route == ModalRoute.of(context)': is not true.

======== Exception caught by rendering library ===================================================== Each child must be laid out exactly once.

======== Exception caught by widgets library ======================================================= The following assertion was thrown building Overlay-[LabeledGlobalKey<OverlayState>#94fb8](state: OverlayState#45c6b(entries: [OverlayEntry#3a155(opaque: true; maintainState: false), OverlayEntry#62084(opaque: false; maintainState: true), OverlayEntry#c051d(opaque: true; maintainState: false), OverlayEntry#ea85f(opaque: false; maintainState: true), OverlayEntry#6b05f(opaque: true; maintainState: false), OverlayEntry#b940f(opaque: false; maintainState: true)])): 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4954 pos 14: '_dependents.isEmpty': is not true.

======== Exception caught by widgets library ======================================================= Duplicate GlobalKey detected in widget tree.

**我尝试过许多解决办法,但都没有奏效**

i根据服务命名密钥差异网

加载项:

代码语言:javascript
运行
复制
class AddUtilityBillState extends State<AddUtilityBill>{
     TextEditingController _nameController = TextEditingController();
  TextEditingController _amountController = TextEditingController();
  TextEditingController _descController = TextEditingController();
  TextEditingController _serviceController = TextEditingController();
  
  GlobalKey<FormState>  utilityFormKey =GlobalKey<FormState>();
  
  @override
  void dispose() {
    utilityFormKey.currentState?.dispose();
    super.dispose();

  }
  
  @override
  Widget build(BuildContext context)  {
    return Hero(
        tag: widget._tag,
        child: Scaffold(
            floatingActionButtonLocation:
                FloatingActionButtonLocation.miniEndTop,
            floatingActionButton: Container(
            
                child:  FloatingActionButton(
                      heroTag: null,
                      onPressed: () {
                        setState(() {
                          if (_validate(context)) {
                            _submit(context);
                          }
                        });
                      },
                      backgroundColor: primaryDarkColor,
                      child: Icon(
                        Icons.done,
                        color: Colors.white,
                      ),
                      elevation: 0,
                      mini: true,
                    )),
            appBar: AppBar(
              backgroundColor: primaryColor,
              shadowColor: Colors.transparent,
              title: widget._from.toString().headText(context: context),
              iconTheme: IconThemeData(color: Colors.white),
            ),
            body:
    Form(
    key: utilityFormKey,
    child:
              _form(),
            )));
  }
  
  form() {
    return  Expanded(
            child: ListView(
          shrinkWrap: true,
          children: [
            '${Strings.utilityBillTypes} :'
                .bigText(context: context, color: Colors.black54, isBold: true)
                .px16(),
            p10.heightBox,
            widget._serviceModel == null
                ? _utilityBillType()
                : widget._serviceModel.categoryType
                    .bigText(context: context, color: Colors.black87)
                    .px16(),
            Visibility(
                visible: _utilityBillTypeErrorVisible,
                child: (context.isMobile ? p5 : p10).heightBox),
            Visibility(
                visible: _utilityBillTypeErrorVisible,
                child: _utilityBillTypeErrorText
                    .normalText(
                      context: context,
                      color: Colors.red,
                    )
                    .objectTopLeft()
                    .px16()),
            p20.heightBox,
            Row(children: [
              Expanded(
                  child: CustomTextField(
                keyboardType: TextInputType.name,
                controller: _nameController,
                labelText: Strings.name,
                hintText: Strings.enterName,
                validator: (String value) {
                  if (value.isEmpty) {
                    return Strings.emptyName;
                  }
                  return null;
                },
              )),
              p20.widthBox,
              Expanded(
                  child: CustomTextField(
                keyboardType: TextInputType.number,
                controller: _amountController,
                labelText: Strings.amount,
                hintText: Strings.enterAmount,
                validator: (String value) {
                  if (value.isEmpty) {
                    return Strings.emptyAmount;
                  }
                  return null;
                },
              ))
            ]).px16(),
            p20.heightBox,
            CustomTextField(
              keyboardType: TextInputType.text,
              controller: _descController,
              labelText: Strings.description,
              hintText: Strings.enterDesc,
            ).px16(),
            p20.heightBox,
            Visibility(
                visible: defaultTargetPlatform == TargetPlatform.android
                    ? _billTypeClicked2
                    : _billTypeClicked,
                child: CustomTextField(
                  keyboardType: TextInputType.text,
                  controller: _serviceController,
                  labelText: defaultTargetPlatform == TargetPlatform.android
                      ? _billTypeItemSelected != null
                          ? _billTypeItemSelected.service
                          : ''
                      : _serviceTypeModelList[_billTypeSelectedIndex].service,
                  hintText: defaultTargetPlatform == TargetPlatform.android
                      ? _billTypeItemSelected != null
                          ? '${Strings.enterData} ${_billTypeItemSelected.service}'
                          : ''
                      : '${Strings.enterData} ${_serviceTypeModelList[_billTypeSelectedIndex].service}',
                  validator: (String value) {
                    if (value.isEmpty) {
                      return Strings.emptyAmount;
                    }
                    return null;
                  },
                ).px16()),
            Visibility(
                visible: (_billTypeClicked2 || _billTypeClicked),
                child: p20.heightBox),
            Strings.reminderDateTime
                .bigText(context: context, color: Colors.black54, isBold: true)
                .px16(),
            p20.heightBox,
            Wrap(children: [
              VxBox(
                  child: Icon(
                Icons.date_range_outlined,
                color: primaryColor,
              )).roundedSM.shadowSm.color(Colors.white).p8.make(),
              p15.widthBox,
              InkWell(
                  onTap: () {
                    FocusScope.of(context).unfocus();
                    _dateCLicked = true;
                    defaultTargetPlatform == TargetPlatform.iOS
                        ? _showDatePicker(context)
                        : _selectDate(context);
                  },
                  child: VxBox(
                          child: (_dateCLicked
                                  ? defaultTargetPlatform == TargetPlatform.iOS
                                      ? _chosenDateTime != null
                                          ? DateFormat(Strings.dateFormat)
                                              .format(_chosenDateTime)
                                              .toString()
                                          : Strings.chooseDate
                                      : "${_selectedDate.toLocal()}"
                                          .split(' ')[0]
                                  : Strings.chooseDate)
                              .bigText(context: context, color: Colors.grey)
                              .objectCenter()
                              .px4())
                      .color(Colors.grey[200])
                      .height(p40)
                      .width(MediaQuery.of(context).size.width / 1.4)
                      .roundedSM
                      .make())
            ]).px24(),
            Visibility(
                visible: _dateErrorVisible,
                child: (context.isMobile ? p5 : p10).heightBox),
            Visibility(
                visible: _dateErrorVisible,
                child: _dateErrorText
                    .normalText(
                      context: context,
                      color: Colors.red,
                    )
                    .objectCenter()
                    .px16()),
            p20.heightBox,
            Wrap(children: [
              VxBox(
                  child: Icon(
                Icons.access_time_outlined,
                color: primaryColor,
              )).roundedSM.shadowSm.color(Colors.white).p8.make(),
              p15.widthBox,
              InkWell(
                  onTap: () {
                    FocusScope.of(context).unfocus();
                    _timeCLicked = true;
                    defaultTargetPlatform == TargetPlatform.iOS
                        ? _showTimePicker(context)
                        : _selectTime(context);
                  },
                  child: VxBox(
                          child:
                              (defaultTargetPlatform == TargetPlatform.iOS
                                      ? _timeIOS != null
                                          ? _timeIOS
                                          : Strings.chooseTime
                                      : _time != null
                                          ? _time
                                          : Strings.chooseTime)
                                  .bigText(context: context, color: Colors.grey)
                                  .objectCenter()
                                  .px4())
                      .color(Colors.grey[200])
                      .height(p40)
                      .width(MediaQuery.of(context).size.width / 1.4)
                      .roundedSM
                      .make())
            ]).px24(),
            Visibility(
                visible: _timeErrorVisible,
                child: (context.isMobile ? p5 : p10).heightBox),
            Visibility(
                visible: _timeErrorVisible,
                child: _timeErrorText
                    .normalText(
                      context: context,
                      color: Colors.red,
                    )
                    .objectCenter()
                    .px16()),
            p40.heightBox,
            Button(
                onTap: () {
                  setState(() {
                    if (_validate(context)) {
                      _submit(context);
                    }
                  });
                },
                title: Strings.submit),
            Visibility(visible: _isLoading, child: p10.heightBox),
            Visibility(
              visible: _isLoading,
              child: CircularProgressIndicator(
                backgroundColor: primaryColor,
              ).objectCenter(),
            ),
            p30.heightBox
          ],
        ));
  }
 
 }
EN

回答 1

Stack Overflow用户

发布于 2022-10-28 12:00:37

虽然这个错误听起来像是命名GlobalKeys的一个问题,但它实际上是一个重复的脚手架。当你使用push导航到第二页时,而不是弹出第二页时,就会出现这种情况,然后尝试将其推回原版。这是在试图创建一个重复的Scaffold。

此外,当在Modal中创建Scaffold时,可能会发生这种情况。在这种特殊的情况下,很难分辨到底发生了什么,但这是两个共同的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66794040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档