_formKey.currentState 始终为 null

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

我遇到以下错误。我在表单中有一个小部件,它是一个动画按钮,我向其传递回调以更新一些数据。当我尝试验证表单是否正确时,我发现在所有情况下 _formKey.currentState 都是 null。

小部件中有两个表单,不知道这是否有问题。

class _CustomerProfileAccountFormState
    extends State<CustomerProfileAccountForm> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<FormState> _paswordFormKey = GlobalKey<FormState>();



Widget build(BuildContext context) {
final delegate = S.of(context);

const EdgeInsets verticalSpaceBetweenFields = EdgeInsets.all(8.0);

return FutureBuilder(
    future: _userProfile,
    builder: (context, AsyncSnapshot<UserProfile> snapshot) {
      if (snapshot.hasData) {
        return Column(children: [
          Form(
            child: Padding(
              padding: const EdgeInsets.all(6),
              key: _formKey,
              child: Column(children: <Widget>[
                Text(
                  delegate.your_account,
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
                Padding(
                  padding: verticalSpaceBetweenFields,
                  child: TextFormField(
                    controller: _formControllers['displayName'],
                    key: _displayNameKey,
                    style: TextinputStyles.standardStyle(context),
                    validator: (value) => Validators.maxLengthValidator(
                        value, displayNameMaxLength),
                    decoration:
                        TextinputStyles.profileInputDecoration(context)
                            .copyWith(labelText: delegate.displayName),
                  ),
                ),
                Padding(
                    padding: verticalSpaceBetweenFields,
                    child: AnimatedLoadingButton(
                      onPressed: _updateUserProfile,
                      initText: delegate.update_profile,
                      doneText: delegate.updated,
                      loadingText: delegate.updating,
                      errorText: delegate.error,
                      buttonState: buttonState,
                    )),
              ]),
            ),
          ),
          Form(
              child: Padding(
                  padding: const EdgeInsets.all(6),
                  key: _paswordFormKey,
                  child: Column(children: <Widget>[...

以及我需要检查表格的功能

  void _updateUserProfile() async {
    try {
      if (_formKey.currentState!.validate()) {
        setState(() {
          buttonState = ButtonLoadingState.loading;
        });

        Map<String, dynamic> data = {
          "displayName": _formControllers['displayName']?.text
        };

        await userProfileVM.updateProfile(widget.uid, data);

        setState(() {
          buttonState = ButtonLoadingState.done;
        });
      }
    } catch (error) {
      setState(() {
        buttonState = ButtonLoadingState.error;
      });

      globals.scaffoldMessengerKey.currentState!
          .showSnackBar(SnackbarsHelper.showSnackbarError(error.toString()));

      debugPrint('Error al obtener usuario: $error');
      rethrow;
    }
  }
flutter dart
3个回答
0
投票

您必须将密钥应用于

Form
,而不是
Padding
小部件。

Form(
   key: _formKey,
   child: ...
)

您对第二种形式也有同样的问题。


0
投票

我发现在所有情况下 _formKey.currentState 都是 null。

这是因为您将

key
传递给了
Padding

 Form(
    child: Padding(
    padding: const EdgeInsets.all(6),
    key: _formKey,

钥匙需要位于

Form
上:

 Form(
    key: _formKey,
    child: Padding(
    padding: const EdgeInsets.all(6),

0
投票

请在表单小部件中传递 _formKey 而不是 padding。

示例:-

Form(

键:_formKey )

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