Home > OS >  Flutter throwing an error when validating form
Flutter throwing an error when validating form

Time:09-24

this is the error its giving me _formKey.currentState.validate : validate is getting the error and it wants me to use null check

The method 'validate' can't be unconditionally invoked because the receiver can be 'null'.

final _formKey = GlobalKey<FormState>();

  moveToHome(BuildContext context) async{
    if(_formKey.currentState.validate())
    {
      setState(() {
        onLog = true;
      });
      await Future.delayed(Duration(milliseconds: 700));
      Navigator.pushNamed(context, MyRoutes.homeRoute);
      setState(() {
        onLog = false;
      });
    }
  }

its also giving me same with my text form field in validator property

TextFormField(
                    decoration: const InputDecoration(
                      hintText: 'Enter username',
                      labelText: 'Username',
                    ),
                    onChanged: (value) {
                      uid = value;
                      setState(() {});
                    },
                    validator: (value) => value.isEmpty? 'Email cannot be empty' : null,
                  )

in validator property isEmpty is also suggesting to use null check

My whole code for clear understanding

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

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  String uid = "";
  bool onLog = false;
  final _formKey = GlobalKey<FormState>();

  moveToHome(BuildContext context) async{
    final f = _formKey.currentState;
    // if(f.validate())
    {
      setState(() {
        onLog = true;
      });
      await Future.delayed(Duration(milliseconds: 700));
      Navigator.pushNamed(context, MyRoutes.homeRoute);
      setState(() {
        onLog = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.white,
      child: SingleChildScrollView(
        child: Form(
          key: _formKey,
            child: Column(
          children: [
            SizedBox(
              height: 20,
            ),
            Image.asset(
              'asset/image/welcome_logo.png',
              fit: BoxFit.cover,
            ),
            const SizedBox(
              height: 20,
            ),
            Text(
              'Hi, $uid',
              style: TextStyle(
                fontSize: 28,
                fontWeight: FontWeight.bold,
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 16, horizontal: 36),
              child: Column(
                children: [
                  TextFormField(
                    decoration: const InputDecoration(
                      hintText: 'Enter username',
                      labelText: 'Username',
                    ),
                    onChanged: (value) {
                      uid = value;
                      setState(() {});
                    },
                    validator: (value) => value == null ? 'Email cannot be empty' : null,
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  TextFormField(
                    obscureText: true,
                    decoration: const InputDecoration(
                        labelText: 'Password', hintText: 'Enter password'
                    ),
                    validator: (value) {
                      if(value == null){
                        return 'Please enter password!';
                      }else if(value.length <6){
                        return 'password must be at least 6 characters';
                      }
                      return value;
                    },
                  ),
                  const SizedBox(
                    height: 30,
                  ),
                  InkWell(
                    onTap: () => moveToHome(context),
                    child: AnimatedContainer(
                      duration: Duration(milliseconds: 200),
                      width: onLog ? 50 : 150,
                      height: 50,
                      alignment: Alignment.center,
                      child: onLog
                          ? Icon(
                              Icons.done,
                              color: Colors.white,
                            )
                          : Text(
                              'Login',
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 20),
                            ),
                      decoration: BoxDecoration(
                        color: Colors.cyan,
                        borderRadius: BorderRadius.circular(onLog ? 50 : 8),
                      ),
                    ),
                  )
                  // ElevatedButton(
                  //     onPressed: () =>{
                  //       Navigator.pushNamed(context, MyRoutes.homeRoute),
                  //     },
                  //     child: const Text('Login'),
                  //     style:TextButton.styleFrom(minimumSize: Size(140, 40)),
                  // )
                ],
              ),
            )
          ],
        )),
      ),
    );
  }
}

 

CodePudding user response:

Concerning GlobalKey.currentState

From GlobalKey.currentState documentation:

The current state is null if (1) there is no widget in the tree that matches this global key, (2) that widget is not a [StatefulWidget], or the associated [State] object is not a subtype of T.

So if you respect those conditions (which I can not deduce from what you have shared), you can safely use the ! operator to ignore the nulability of GlobalKey.

Concerning validator.value

This is due to the fact that TextFormField inherent from FormField which itself can have a null initial value. However if you look at the implementation of TextFormField is never null (since the default value is set to '' if null is given). Therefore here you can use the ! operator safely.

Null safety in general

If you are having trouble understanding the concept of null safety in its own, I would advise you to do some research since this is a vast but powerful concept. Here is somewhere you could start: https://dart.dev/null-safety

  • Related