flutter 无法将元素类型“MatchValidator”分配给列表类型“FieldValidator< dynamic>”

mwngjboj  于 2023-03-04  发布在  Flutter
关注(0)|答案(1)|浏览(97)

我是新手。
我不明白为什么会出现此错误:无法将元素类型“MatchValidator”分配给列表类型“FieldValidator”。
我在重置密码屏幕中使用了form_field_validator包。
在TextFormField Validator中,我正在使用MultiValidator(),其中RequiredValidator、MaxLengthValidator、MinLenghtValidator工作正常,但当我使用MatchValidator时,它导致错误“"”“""元素类型”MatchValidator“无法分配给列表类型”FieldValidator“。”“”“”“”
有谁能描述一下

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:form_field_validator/form_field_validator.dart';

import 'package:my_treat/common/constants.dart';
import 'package:my_treat/common/common.dart';
import 'package:my_treat/common/AppTheme.dart';
import 'package:my_treat/screens/login_screen.dart';

class ResetPasswordScreen extends StatefulWidget {
  const ResetPasswordScreen({Key? key}) : super(key: key);

  static const routeName = 'reset-password';

  @override
  State<ResetPasswordScreen> createState() => _ResetPasswordScreenState();
}

class _ResetPasswordScreenState extends State<ResetPasswordScreen> {
  bool isHidden1 = true;
  bool isHidden2 = true;

  GlobalKey<FormState> formkey = GlobalKey<FormState>();

  void togglePasswordView1() {
    setState(() {
      isHidden1 = !isHidden1;
    });
  }

  void togglePasswordView2() {
    setState(() {
      isHidden2 = !isHidden2;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        padding: EdgeInsets.only(left: 20, right: 20),
        decoration: Common.commonBoxDecoration,
        child: Form(
          key: formkey,
          child: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(height: Common.displayHeight(context) * 0.04),
                Common.backButton(() {
                  Navigator.of(context).pop();
                }),
                SizedBox(height: Common.displayHeight(context) * 0.02),
                Image(image: AssetImage('assets/images/my-treat.png')),
                SizedBox(height: Common.displayHeight(context) * 0.06),
                Align(
                  child: Text(
                    'Reset Password',
                    style: TextStyle(fontSize: 23, color: AppColors.white),
                  ),
                ),
                SizedBox(height: Common.displayHeight(context) * 0.07),
                TextFormField(
                  cursorColor: AppColors.darkGrey,
                  maxLength: 15,
                  keyboardType: TextInputType.visiblePassword,
                  textInputAction: TextInputAction.next,
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(
                        RegExp("[a-zA-Z0-9!@#\$%^&*.]")),
                  ],
                  obscureText: isHidden1,
                  validator: MultiValidator(
                    [
                      RequiredValidator(errorText: 'Required'),
                      MaxLengthValidator(15,
                          errorText: 'Should Not Greater than 15 Characters'),
                      MinLengthValidator(7,
                          errorText: 'Should Be Greater than 7 Characters'),
                      // MatchValidator(errorText: 'sdf'),
                    ],
                  ),
                  decoration: InputDecoration(
                    counterText: '',
                    prefixIcon: Icon(
                      Icons.lock_open_sharp,
                      color: AppColors.darkGrey,
                    ),
                    hintText: 'Password',
                    suffixIcon: IconButton(
                      icon: Icon(
                          isHidden1 ? Icons.visibility : Icons.visibility_off),
                      color: AppColors.lightGrey,
                      onPressed: togglePasswordView1,
                    ),
                    fillColor: AppColors.white,
                    filled: true,
                    border: Common.commonOutlineInputBorder,
                  ),
                ),
                SizedBox(height: Common.displayHeight(context) * 0.02),
                TextFormField(
                  cursorColor: AppColors.darkGrey,
                  maxLength: 15,
                  keyboardType: TextInputType.visiblePassword,
                  textInputAction: TextInputAction.done,
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(
                        RegExp("[a-zA-Z0-9!@#\$%^&*.]")),
                  ],
                  validator: MultiValidator(
                    [
                      RequiredValidator(errorText: 'Required'),
                      MinLengthValidator(7,
                          errorText: 'Should Be Greater than 7 Characters'),
                      MaxLengthValidator(15,
                          errorText: 'Should Not Greater than 15 Characters')
                    ],
                  ),
                  obscureText: isHidden2,
                  decoration: InputDecoration(
                    counterText: '',
                    prefixIcon: Icon(
                      Icons.lock_open_sharp,
                      color: AppColors.darkGrey,
                    ),
                    hintText: 'Confirm Password',
                    suffixIcon: IconButton(
                      icon: Icon(
                          isHidden2 ? Icons.visibility : Icons.visibility_off),
                      color: AppColors.lightGrey,
                      onPressed: togglePasswordView2,
                    ),
                    fillColor: AppColors.white,
                    filled: true,
                    border: Common.commonOutlineInputBorder,
                  ),
                ),
                SizedBox(height: Common.displayHeight(context) * 0.12),
                SizedBox(
                  height: Common.displayHeight(context) * 0.07,
                  child: Common.customElevatedButton(
                    Constants.update,
                    () {
                      if (formkey.currentState!.validate()) {
                        Navigator.of(context).pushNamedAndRemoveUntil(
                            LoginScreen.routeName,
                            (Route<dynamic> route) => false);
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

b4wnujal

b4wnujal1#

看起来您没有正确使用匹配验证器。根据软件包文档,您应该按如下方式使用MatchValidator:

validator: (val) => MatchValidator(errorText: 'passwords do not match')
                    .validateMatch(val!, _passwordController.text.trim()),

希望能有所帮助!

相关问题