I got the same problem with null statements (?) in Dart multiple times in different cases. I really hope somebody can help me.

Just added some lines of code & the error:

Error:

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 ('!'). here

Here is one of my examples:

child: MaterialButton(
                          onPressed: () {
                            var currentState = this._formKey.currentState;
                            if (currentState == null) {
                              return;
                            }
                            if (_formKey.currentState.validate()) {
                              AuthService.instance.signIn(
                                  email: emailTextEditingController.text,
                                  password:
                                      passwordTextEditingController.text);
                              if (AuthService.instance
                                      .checkIfUserExists() ==
                                  true) {
                                Navigator.pushReplacement(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => MainMenu()));
                              } else {
                                Navigator.pushReplacement(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) =>
                                            VerifyScreen()));
                              }
                            }
                          },

Got this error-message again:

The method 'validate' can't be unconditionally invoked because the receiver can be 'null'.Try making the call conditional (using '?.') or adding a null check to the target ('!').

After I edited the code with a ! to avoid the Null-Statement like:

singUpUser() {
if (formKey.currentState!.validate()) {
  setState(() {
    isLoading = true;
  });
} else {
  return null;
};

But now i just avoid the error in the code itself, after starting a emulator and testing it, next error appears:

Null check operator used on a null value

So thats not the right solution...

If you need more code, just message me.

Thank you!

Tom


Solution 1: Tolga Kartal

Being empty is not the same as being null. So before you can check an object is empty, you need to check against null first.

if (obj != null && !obj.isEmpty) {}


Solution 2: venir

In a nutshell: if Dart is certain that a variable at compile time can be null at runtime, it doesn't compile.. unless you explicitly check for null values, and/or promote the variable to be non-nullable with the ! operator (Dart is not always able to infer the non-nullability in certain situations, so it's our responsibility to promote them to non-nullable).

There's much more to know if you're curious ("why?", for starters), so I'd suggest to check the null safety documentation (quick read).

This being said, your code now changes:

(1) We must check if val is nullable, before accessing it. We can either use ! or .? to safely access it; note: the null check operator ! is the least safe null operator, it's likely that it will result in run time exceptions.

validator: (val) {
  val==null || val?.isEmpty || val?.length<3
  ? "Enter Username 3+ characters"
  : null
}

(2) I can't infer which method / variable can be null by myself

(3) It depends on what you're trying to do, but I guess that you're trying to implement the Firebase authentication process, in which your user can and should be null before authenticating. Therefore, your function should accept a nullable user value (User?). In there, we do the usual null check and we add a ! operator to promote its value in case user is not null. As aforementioned, Dart isn't always able to infer nullability of variables.

MyUser _userFromFirebaseUser(User? user) {
   return user==null ? null : MyUser(userId: user!.uid);
}

Note how using a null-check ! here is perfectly safe, only because you just checked its nullability in the same line (nonetheless, keep a wise-eye when you refactor this).

EDIT. (4) I can't infer where exactly your exception is fired, but since you want to validate your form, then here's my code from a project of mine:

// inside "saveForm()"...
var currentState = this._formKey.currentState;
if (currentState == null)
  return; // this just means something went wrong
if (!currentState.validate()) return; // todo if invalid, handle this, maybe show a snackbar and stuff...

Note how the variable currentState is now promoted to be non-nullable WITHOUT using the null check operator !, which is just good practice (avoid using ! whenever possible, PREFER using null-aware operators, such as ?. or ??, or ?=)