why is the null-check of an outer variable isn't enough in dart?

117

Since this variable is not an inline variable, we can't be sure that it will not change between checking and using. You may be calling another function inside your condition block and that function sets that global variable to null. So to have sound null-safety type promotion only works on inline variables.

In your case, you can use fab?.call() without checking the variable isn't null or fab!() inside your condition block. Read more here.

Share:
117
Aegletes
Author by

Aegletes

Updated on January 03, 2023

Comments

  • Aegletes
    Aegletes over 1 year

    Why does the below code give an error?

    
    Function? fob;
    
    void someMethod() {
        if(fob != null) {
            fob();
        }
    }
    

    Why is this null-check not enough and fob(); here gives an error? What could be happening between the if check and the call to the function that it could be null again?

    I know this works when I declare a local variable to the function, but I just want to understand why dart works the way it does.

    • Josteve
      Josteve over 2 years
      @jamesdlin please check.
    • Daniel
      Daniel over 2 years
      Have a look at this answer. It applies for your case as well.
    • Aegletes
      Aegletes over 2 years
      Thank you @Daniel but I am still not exactly sure about the reasoning here. Could you explain it to me? I've seen the post you sent and it talks about getters possibly changing the variable to null, right? Why is dart not able to check this though? What could a getter be doing there, is there any example about this?
  • Aegletes
    Aegletes over 2 years
    Thank you for your answer @Amir_P, I know I can do a fab?.call() in this case, but I was just giving a simple example, wanted to know why this is happening. So what you are saying is basically, if the variables gets set to null even through a function that is called within the initial function, the compiler has no way of knowing this, right? From the link you sent, I guess they are still kinda looking into it.
  • Amir_P
    Amir_P over 2 years
    Right, compiler has no way to be sure that it's not null when you're using it. As far as I found out, solutions are very limited. Some cases can be handled like final variables or there may be a way to implicitly force non-nullability to your expressions after checking if it's not null but I guess the way it's working now is as expected and clear. @Aegletes
  • Aegletes
    Aegletes over 2 years
    Got it, thank you for your nice explanation, I appreciate it @Amir_P
  • Aegletes
    Aegletes over 2 years
    I avoid using ! in general if I can, other devs work on your code and you can never know if they would make changes inside your if that would cause the variable to become null, since the compiler is also unaware of this, I think it's dangerous to use this in general. I was mostly asking the reason behind why it works this way, @Amir_P's explanation was good, you can't 100% be sure that it's not null, neither can the compiler.
  • JIJO J
    JIJO J over 2 years
    There are so many use-cases that you definitely need to use ! operator. In those cases, we devs should make sure that the variable can't be null for 100%.