value.isEmpty is throwing error in when i try to compile the code

Viewed 1142
 TextFormField(
                        obscureText: true,
                        decoration: InputDecoration(
                          hintText: "Enter password",
                          labelText: "Password",
                        ),
                        validator: (value) {
                          if (value.isEmpty) {
                            return "Password can't be empty";
                          }
                          null;
                        },
                      ),

I am watching a tutorial of flutter and i know its too basic but seems like , I can't fix this error I tried to search it but there is no proper guide to fix can someone help me in fixing this when its working fine on tutorial but not for me , and i don't get it yet its showing The property 'isEmpty' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!'). this error

2 Answers

This is because value is of type String? and can be null. In you case you have to check if value is null.

 TextFormField(
                        obscureText: true,
                        decoration: InputDecoration(
                          hintText: "Enter password",
                          labelText: "Password",
                        ),
                        validator: (String? value) {
                          if (value!.isEmpty) {
                            return "Password can't be empty";
                          }
                          null;
                        },
                      ),

Welcome to stackoverflow and flutter. Recently flutter has been updated to flutter 2.0, which has a great feature called null safety. It is a little annoying to get started with, but has huge benefits I am sure you can find better listed elsewhere. Since null safety has recently come out, its not in most tutorials. Its job is to ensure that whenever there is a chance for a value to be null, it asks you to either say
A. Check if the value is becoming null. If it is, give an error and cancel the function
B. Allow the value to be null because you are going to be keeping track.\

In your case, the variable value in the onchanged method may be null because the flutter people who wrote it, used the B part mentioned above for their internal building. So, what you have to do is, the A part, that is checking if the value is null or not. You can simply do that with the help of an !. Now, you have to be careful on how to use this. Whenever a variable can be null and you have to perform a function on it or assign it to a variable which cannot be null, you just put an exclamation mark after it. Like value!\

I would recommend watching a good video on null safety in flutter and learn about it since I doubt you will find a good tutorial as of now that includes null safety (except the docs ofc). if you need any help please feel free to leave a comment and I'll be happy to help in any way possible

Related