Home > OS >  Problem with using a form validator in Flutter (dart)
Problem with using a form validator in Flutter (dart)

Time:03-17

I was trying to implement a login form in flutter watching this video (minute 20:00) (https://www.youtube.com/watch?v=GFKqoIAPd0Q) and everything went great until this part:

class InputText extends StatelessWidget {
  final String label;
  final String hint;
  final Icon icono;
  final TextInputType keyboard;
  final bool obsecure;
  final void Function(String data) onChanged;
  final String Function(String data) validator;
  const InputText({ Key? key, 
      this.label = '',
      this.hint = '',
      required this.icono,
      this.keyboard = TextInputType.text,
      this.obsecure = false,
      required this.onChanged,
      required this.validator}) 
       : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextFormField(
        keyboardType: this.keyboard,
        obscureText: this.obsecure,
        onChanged: this.onChanged,
        validator: validator,                      // THIS LINE RIGHT HERE
        decoration: InputDecoration(
          hintText: this.hint,
          labelText: this.label,
          labelStyle: TextStyle(

The validator there gives an error, but in the video it worked fine. I'm currently working with Flutter version 2.10.3 and Dart SDK version 2.16.1. Does anyone know how to solve this? Thanks in advance

CodePudding user response:

Change declaration of the validator to:

final String? Function(String? data) validator;
  • Related