I do all possible to solve that error Null check operator used on a null value I use (!) but there is error

Viewed 32
class login extends StatelessWidget {
  var emailController = TextEditingController();

  var PasswordController = TextEditingController();

  var _formKey = GlobalKey<FormState>();

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Login"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: SingleChildScrollView(
          child: Center(
            child: Form(
              key: _formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("Login",
                      style: TextStyle(
                          fontSize: 40, fontWeight: FontWeight.bold)),
                  SizedBox(height: 40,),

                  TextFormField(

                    controller: emailController,
                    onFieldSubmitted: (String value) {
                      print(value);
                    },
                    onChanged: (String value) {
                      print(value);
                    },
                    validator: (String ?value) {
                      if (value == null || value.isEmpty) {
                        return 'the password must not be Empty';
                      }
                      return null;
                    },

                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                      labelText: "E-mail Address",
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(10)),
                      prefixIcon: Icon(Icons.email),
                    ),

                  ),
                  SizedBox(height: 20,),
                  TextFormField(
                      controller: PasswordController,
                      obscureText: true,
                      keyboardType: TextInputType.visiblePassword,
                      validator: (String ?value) {
                        if (value == null || value.isEmpty) {
                          return 'the password must not be Empty';
                        }
                        return null;
                      },
                      onFieldSubmitted: (String value) {
                        print(value);
                      },
                      onChanged: (String value) {
                        print(value);
                      },

                      decoration: InputDecoration(
                          labelText: "Password",
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10)),
                          prefixIcon: Icon(Icons.lock),
                          suffixIcon: Icon(Icons.remove_red_eye_rounded)
                      )

                  ),
                  SizedBox(height: 20,),
                  defaultButton(
                      background: Colors.black26,
                      isUpperCase: true,
                      text: "Login",
                      function: () {
                       if (_formKey.currentState!.validate()) ==> // Null check operator 
                                                                    used on a null value.
                        {
                          print(emailController);
                          print(PasswordController);
                        }
                      }
                  ),
                  SizedBox(height: 20,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text("Already you Have account?"),
                      TextButton(
                          onPressed: () {
                            Navigator.push(context,
                                MaterialPageRoute(
                                    builder: (context) => Registry()));
                          },
                          child: Text("Register now "))
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
1 Answers

Hey what you're using is called the non-null assertion operator, it actually means you are saying that value can't be null. If you want to make sure it's not null before trying to access validate(), you should use optional chaining _formKey.currentState?.validate() What you can also try to do is check that it's not null before you call validate().

if (!!_formkey.currentState && _formKey.currentState.validate())

Related