admin管理员组

文章数量:1278856

I am using formkey in my screen and I am not reusing it anywhere . But getting following error on navigation. I am defining my formkey inside my viewmodel. Putting this inside view resolves problem. But I dont want to change it from view model.

Duplicate GlobalKey detected in widget tree. The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous instance is moved to the new location. The key was:

  • [LabeledGlobalKey#78a40 resetKey] This was determined by noticing that after the widget with the above global key was moved out of its previous parent, that previous parent never updated during this frame, meaning that it either did not update at all or updated before the widget was moved, in either case implying that it still thinks that it should have a child with that global key. The specific parent that did not update after having one or more children forcibly removed due to GlobalKey reparenting is:
  • KeyedSubtree-[GlobalKey#d59ba] A GlobalKey can only be specified on one widget at a time in the widget tree.
    GlobalKey<FormState> resetPasswordFormKey =
        GlobalKey<FormState>(debugLabel: 'resetKey');

    class ResetPasswordView extends ConsumerStatefulWidget {
        final String? token;
        const ResetPasswordView({super.key, this.token});

        @override
        ConsumerState<ConsumerStatefulWidget> createState() =>
            _ResetPasswordViewState();
    }

    class _ResetPasswordViewState extends ConsumerState<ResetPasswordView> {
        @override
        void initState() {
            super.initState();
            WidgetsBinding.instance.addPostFrameCallback((_) {
                final authVM = ref.read(authVMProvider);
                if (widget.token != null && widget.token!.isNotEmpty) {
                    authVM.setToken(widget.token!);
                }
            });
        }

        @override
        Widget build(BuildContext context) {
            final authVM = ref.watch(authVMProvider);
            widget.token?.isNotEmpty ?? authVM.setToken(widget.token);
            return SafeArea(
                child: Scaffold(
                    body: Form(
                        key: authVM.resetPasswordFormKey,
                        child: SingleChildScrollView(
                            reverse: true,
                            child: Padding(
                                padding: EdgeInsets.only(
                                    top: context.width * 0.18,
                                    left: context.width * 0.10,
                                    right: context.width * 0.10,
                                ),
                                child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                        if (!authVM.isResetPasswordFromProfile)
                                            Row(
                                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                children: [
                                                    Image.asset(
                                                        K.appIconBlack,
                                                        height: context.height * 0.06,
                                                        fit: BoxFit.fill,
                                                    ),
                                                    IconButton(
                                                        padding: EdgeInsets.zero,
                                                        onPressed: () {
                                                            ref
                                                                .read(authVMProvider)
                                                                .clearResetPasswordView(context);
                                                        },
                                                        icon: Icon(Icons.close),
                                                    )
                                                ],
                                            ),
                                        Text(
                                            KStrings.resetYourPassword,
                                            style: AppTextStyles.gantariBold.copyWith(
                                                color: AppColors.textBlack,
                                                fontSize:
                                                    !ref.read(authVMProvider).showInstructionText() &&
                                                            !authVM.isResetPasswordFromProfile
                                                        ? 20
                                                        : 36),
                                            textAlign: authVM.isResetPasswordFromProfile
                                                ? TextAlign.center
                                                : TextAlign.start,
                                            maxLines: 2,
                                        ),
                                        if (ref.read(authVMProvider).showInstructionText() &&
                                            authVM.isResetPasswordFromProfile)
                                            SizedBox(height: 10),
                                        if (ref.read(authVMProvider).showInstructionText() &&
                                            !authVM.isResetPasswordFromProfile)
                                            Text(
                                                KStrings.passwordUpdateInstruction,
                                                style: AppTextStyles.gantariRegular
                                                    .copyWith(color: AppColors.textBlack, fontSize: 18),
                                                textAlign: TextAlign.start,
                                                maxLines: 6,
                                            ),
                                        if (ref.read(authVMProvider).showInstructionText() &&
                                            authVM.isResetPasswordFromProfile)
                                            SizedBox(height: 10),
                                        if (authVM.isResetPasswordFromProfile)
                                            Text(
                                                KStrings.thanksText,
                                                style: AppTextStyles.gantariRegular.copyWith(
                                                    color: AppColors.textLightBlack, fontSize: 20),
                                                textAlign: TextAlign.start,
                                                maxLines: 3,
                                            ),
                                        SizedBox(height: 12),
                                        if (!authVM.isResetPasswordFromProfile)
                                            InputFieldComponent(
                                                textFieldHeight: 56,
                                                title: KStrings.currentPassword,
                                                titlePadding: EdgeInsets.only(bottom: 5),
                                                controller: authVM.currentPasswordController,
                                                inputTextStyle:
                                                    AppTextStyles.gantariMedium.copyWith(fontSize: 14),
                                                onChange: (value) {
                                                    authVM.validatePassword(value!);
                                                },
                                                focusedBorder: OutlineInputBorder(
                                                    borderSide: BorderSide(
                                                        color:
                                                            authVM.currentPasswordController.text.isNotEmpty
                                                                ? AppColors.green
                                                                : AppColors.black,
                                                        width: 1.5),
                                                ),
                                                textFieldBorder: OutlineInputBorder(
                                                    borderSide: BorderSide(
                                                        width: 1,
                                                        color:
                                                            authVM.currentPasswordController.text.isNotEmpty
                                                                ? AppColors.green
                                                                : AppColors.textFieldBorderColor,
                                                    ),
                                                    borderRadius: BorderRadius.all(Radius.circular(4.0)),
                                                ),
                                                keyboardType: TextInputType.text,
                                                validator: (value) {
                                                    return (value == null || value.isEmpty)
                                                        ? KStrings.currentPasswordIsRequired
                                                        : null;
                                                },
                                                maxLines: 1,
                                                suffixIcon:
                                                    authVM.currentPasswordController.text.isNotEmpty
                                                        ? Icon(
                                                            Icons.check,
                                                            color: AppColors.green,
                                                        )
                                                        : null,
                                                isObsecure: true,
                                            ),
                                        const SizedBox(height: 20),
                                        ButtonWidget(
                                            borderRadius: BorderRadius.circular(4),
                                            buttonText: KStrings.continueString,
                                            isEnabled: authVM.isResetPasswordValid,
                                            onPressed: () async {
                                                if ((authVM.resetPasswordFormKey.currentState
                                                        ?.validate() ??
                                                    false)) {
                                                    if (authVM.isResetPasswordFromProfile) {
                                                        authVM.updatePasswordLinkFromLogin(context);
                                                    } else {
                                                        authVM.resetPasswordFromProfile(
                                                            authVM.newPasswordController.text,
                                                            authVM.currentPasswordController.text,
                                                        );
                                                    }
                                                }
                                            },
                                        ),
                                    ],
                                ),
                            ),
                        ),
                    ),
                ),
            );
        }
    }

本文标签: formsDuplicate GlobalKey detected in widget tree FlutterStack Overflow