TextFormField validator The argument

Issue

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);

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

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 "";
      },
     )

Solution

Replace this line :

final Function() validator;

To:

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 *