Home > OS >  TextFormField validator The argument
TextFormField validator The argument

Time:10-21

i am try to make custom widget TextFormField, but the validator: validator, show error

The argument type 'dynamic Function()' can't be assigned to the parameter type 'String? Function(String?)?'.dartargument_type_not_assignable dynamic Function() validator package:store/widgets/mytextformfield.dart

mytextformfield.dart <<< file widget TextFormField

import 'package:flutter/material.dart';



class MyTextFormField extends StatelessWidget {
  final Function() validator;
  final bool obserText;
  final String name;

  const MyTextFormField({
    Key? key,
    required this.obserText,
    required this.validator,
    required this.name,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      obscureText: obserText,
      **validator: validator,**
      decoration: InputDecoration(
        border: const OutlineInputBorder(),
        hintText: name,
      ),
    );
  }
}

on login.dart

MyTextFormField(
      obserText: obserText,
      name: "Email",
          validator: (value) {
          if (value == "") {
         return "Please Fill Email";
        } else if (!regExp.hasMatch(value!)) {
          return "Email is Invaild";
        }
      return "";
      },
     )

CodePudding user response:

Try to define only as Function instead of Function():

final Function validator;

CodePudding user response:

Replace this line :

final Function() validator;

To:

final FormFieldValidator<String>? validator;

CodePudding user response:

Change:

final Function() validator;

With:

final FormFieldValidator<String>? validator

As it is declared in TextFormField class

  • Related