TextFormField validator The argument


I am trying to make a custom widget TextFormField, but the validator: validator, show an 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);

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


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


Replace this line :

final Function() validator;


final FormFieldValidator<String>? validator;

Answered By – dangngocduc

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *