Home > Enterprise >  Give me error validator in undefinded but i used globel key in flutter help me to debug this error
Give me error validator in undefinded but i used globel key in flutter help me to debug this error

Time:02-08

Undefined named parameter error occurred when I used validator function in form text Field

I used the Global key and want to validate my form text field but when using the validator function compiler show the error validator is undefined. Help me, please my code in a flutter as follow.

import 'package:flutter/material.dart';

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

  @override
  State<LoginScreen> createState() => _LoginScreen();
}

class _LoginScreen extends State<LoginScreen> {
  //GlobalKey<FormState> formKey = GlobalKey();
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //child: Text('This is bakwas container'),
        appBar: AppBar(
          title: const Text('Login'),
        ),
        body: Form(
          key: _formKey,
          //validator:(){},
          child: Center(
            child: SizedBox(
              width: MediaQuery.of(context).size.width * .7,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const TextField(
                  // validator: (value) {
                    //   if (value == null || value.isEmpty) {
                    //     return 'Please enter some text';
                    //   }
                    //   return null;
                    // },
                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                        labelText: 'Email', hintText: '[email protected]'),
                  ),
                  //const SizedBox(height: 10,)
                  const TextField(
                    obscureText: true,
                    keyboardType: TextInputType.visiblePassword,
                    decoration: InputDecoration(labelText: 'Password'),
                  ),
                  Container(
                      margin: const EdgeInsets.only(top: 10),
                      child: ElevatedButton(
                          onPressed: () {}, child: const Text('Press IT')))
                ],
              ),
            ),
          ),
        ));
  }
}

CodePudding user response:

To use validator you need to replace TextFiled with TextFormField.

TextFormField(
  validator: (value) {
    if (value == null || value.isEmpty) {
      return 'Please enter some text';
    }
    return null;
  },
  keyboardType: TextInputType.emailAddress,
  decoration: InputDecoration(
      labelText: 'Email', hintText: '[email protected]'),
),

CodePudding user response:

If you are using TextField your validation should look like below

class _MyHomeScreenState extends State<MyHomeScreen> {
 final _userEmailController = TextEditingController();
 void errorDialog(String title, String msg) {
 showDialog(
  context: context,
  builder: (_) => AlertDialog(
    title: Text(
      title,
      style: const TextStyle(fontWeight: FontWeight.bold),
    ),
    content: Text(
      msg,
      textAlign: TextAlign.center,
    ),
    actions: [
      TextButton(
        onPressed: () => Navigator.pop(context),
        child: const Text('OK'),
      )
    ],
  ),
 );
}
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(title: const Text("Test"),),
  body: Column(
    children: [
      TextField(
        controller: _userEmailController,
        decoration: const InputDecoration(
          hintText: '[email protected]',
        ),
      ),
      ElevatedButton(
        onPressed: () {
          if (_userEmailController.text.isEmpty) {
            errorDialog('Message', 'User email can not be empty!');
          }
        },
        child: const Text(
          'Login',
          style: TextStyle(fontSize: 18),
        ),
      ),
    ],
  ),
);
}
}
  •  Tags:  
  • Related