在小部件树中检测到重复的 GlobalKey,并且每个子项必须仅布置一次

时间:2021-03-25 06:14:42

标签: flutter

enter code here我有抽屉类,其中包含带有服务列表的多页,并且在每个页面内我都添加了服务页面 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.

**我尝试了很多解决方案,但都没有奏效**

我根据服务命名了key不同

添加页面:

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
          ],
        ));
  }
 
 }

0 个答案:

没有答案