-1

I have just upgraded to sound null safety and having trouble making this condition work. How can I check the length on a nullable variable?

Though the error makes total sense, if the event.newPhone is null it cannot perform the check, any suggestion for a better approach for this?

What I have:

if(event is AccountInfoChanged){
 if(event.newPhone?.length > 7){ -----> the issue is on ">" operator
    yield EnableUpdateButton();
  }
}
Manas
  • 3,060
  • 4
  • 27
  • 55

6 Answers6

4

You should use local variable to cast away nullability.

if (event is AccountInfoChanged) {
  var newPhone = event.newPhone;
  if (newPhone != null && newPhone.length > 7) {
    yield EnableUpdateButton();
  }
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
0

Try it like this:

if(event is AccountInfoChanged)
  if(event.newPhone != null) 
  //to check if phone is null or not, before performing operations on it
    if(event.newPhone!.length > 7) { --> at this point I had to add ! as it is not null for sure 
    yield EnableUpdateButton();
  }

Manas
  • 3,060
  • 4
  • 27
  • 55
Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
  • I tried this as well, still shows the error on the `>` – Manas Aug 04 '21 at 10:33
  • 1
    If you always sure that there is a phone number, add `!` to newPhone and length. – Huthaifa Muayyad Aug 04 '21 at 10:36
  • @Manas Look at this [answer](https://stackoverflow.com/a/65035575/12603592) – PatrickMahomes Aug 04 '21 at 10:37
  • @HuthaifaMuayyad the issue is the phone number should be nullable as well in some cases – Manas Aug 04 '21 at 10:54
  • 1
    @Manas As explained by @PatrickMahomes comment, you need to use `event.newPhone!.length > 7` *after* checking `if(event.newPhone != null)`. A null check will not promote a non-local variable. – jamesdlin Aug 04 '21 at 12:55
  • @jamesdlin & @PatrickMahomes perfect! Thank you this works well. so I. first checked if it's not null first before checking the length, the only thing missing in @huthaif-muayyad is the `!` on the `event.newPhone!.length` – Manas Aug 05 '21 at 04:32
  • @HuthaifaMuayyad could you please update the answer on the second check as `event.newPhone!.length > 7` pls, so I can mark it as the correct answer – Manas Aug 05 '21 at 04:35
0

Try to update the condition like this (event.newPhone?.length ?? -1) > 7 so the left side of the > operator always have a non null value. If the event.newPhone is null the left side value will be -1. You can update this value according to your application needs.

if((event.newPhone?.length ?? -1) > 7){
    yield EnableUpdateButton();
}
sajalsd
  • 349
  • 1
  • 5
0

It depends on your context there, but in a nutshell: you've got to choose what to do when even.netPhone is null.

Dart is able, in some contexts, to do the type promotion by itself: here's a quick dartpad example (as you can see, no compile-time errors happen):

void main() {
  int? a;
  
  a = doSomeWork();
  
  if (a == null) {
    print("do something when a is null");
    return;
  }
  if (a.isEven) {
    print("do something even");
  } else if (a.isOdd) {
    print("do something odd");
  }
}

int? doSomeWork() {
  return 5;  // You can change this to any int? value
}

Therefore, in your code you could do something like this:

    var myPhone = event.newPhone;
    if (myPhone == null) {
      print("Do something when there's no phone");
      return;
    }
    if (myPhone.length > 7) {  // No null-aware operators needed
      print("Actions when the phone is not null");
    }

As you can see, the return statement lets Dart infer the type promotion. If you can't return (again, depends on your logic), use the ! operator to help it do so.

IMPORTANT NOTE / EDIT. As suggested by @jamesdlin, This only works with a local variable. At the moment I'm writing this, there are several Dart open issues on github to let Dart infer more about non-nullability promotion.

venir
  • 1,809
  • 7
  • 19
  • 1
    Automatic type promotion does not occur for non-local variables, so you will need to use `event.newPhone!.length > 7` after the `if (event.newPhone == null)` check. – jamesdlin Aug 04 '21 at 13:22
-2

Type Cast it to non nullable int after you do a null check

if(event.newPhone?.length as int >7){//your code}
  • An explicit cast to `int` is inappropriate since the use of `?.` indicates that `event.newPhone` is expected to be `null` in some circumstances, and the cast would lead to a `TypeError` being thrown at runtime. – jamesdlin Aug 04 '21 at 13:20
  • That's the reason why I've mentioned the fact that they only do it after a null check. – Nabhan Hanif Aug 04 '21 at 13:47
-2

Add "!" near length to make it non null condition.

if(event is AccountInfoChanged){
if(event.newPhone?.length! > 7){ -----> the issue is on ">" operator
yield EnableUpdateButton();
 }
}
Jashwanth Neela
  • 263
  • 1
  • 8
  • Unless `.length` returns a nullable type (which wouldn't be possible if `newPhone` is a `String` or `Iterable`), then `event.newPhone?.length!` is the same as `event.newPhone?.length`. Even if you mean `(event.newPhone?.length)!`, that still would be inappropriate since the use of `?.` indicates that `event.newPhone` is expected to be `null` in some circumstances. – jamesdlin Aug 04 '21 at 13:19