23

Just started Flutter with native iOS background, so I just have a quick question about Dart beta null safety.

So in Swift, because they have the idea of null safety from the beginning just like Kotlin, there are 2 features that I really like about the language is if let and guard let. These 2 make working with optional values so much easier. I'm not sure if the beta version of Dart has anything like that.

Thanks

Son Nguyen
  • 1,124
  • 2
  • 10
  • 24

5 Answers5

23

I'm not an expert on Swift, but Dart will use null checks to automatically promote types, and I think that mostly does the job of if let and guard let.

For example:

String? x = possiblyReturnsNull();
if (x != null) {
  // All code within this block treats `x` as non-nullable.
}
// All code outside the block continues to treat `x` as nullable.

Note that promotion won't be performed on non-local variables, so for those you would need to explicitly introduce a local reference. (There is a language proposal to provide a mechanism to allow a nicer mechanism to add a local reference without polluting the outer scope.)

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
7

I'm going to jump in on this, since I'm coming from Swift too and love to use guard a lot. Adding to what @jamesdlin said, the opposite is also true.

So you can functionally do a Swift guard statement:

String? x = possiblyReturnsNull();
if (x == null) return whatever; // This works like Swift's guard
// All code outside the block now treats `x` as NON-nullable.
RogerTheShrubber
  • 986
  • 8
  • 19
1

A small extension to the accepted answer is that Flutter also allows force-unwrapping an Optional. So in the case you are accessing a non-nil value that is not saved inside a variable, like in a dictionary, you need to unwrap it inside the if-statement:

if (someDict[someKey] != null) {
  print(someDict[someKey]!)
}
//
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
-1

Flutter 3.x

A swifty way to handle this would be an extension on String? to check for null and empty

extension NullEmptyCheck on String? {
  bool notNullOrEmpty() => (this != null && this!.isNotEmpty);
}

Since the null check happens first, it's safe to force unwrap the second this

Usage:

if (myOptionalStr.notNullOrEmpty()) {
  // value is safe to force unwrap
  print(myOptionalStr!);
}

If you're just wanting to unwrap the optional with either the value or an empty String, then you can do something like this

extension Unwrapped on String? {
  String unwrapped() => this != null ? this! : '';
}

That would return the unwrapped value or an empty String

Both can be made generic, though you'd need to pass a default value for the latter which you'd have to ensure would work in all cases

D. Greg
  • 991
  • 1
  • 9
  • 21
-4

To check null safety in Dart:

value ?? 0
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • no, I mean with `if let` you have a new non-null value in the code block, and `guard let` you have the non-null value for the rest of the function body. We can check for the value is null or not in Dart, but then in the code, it is still being treated as nullable. – Son Nguyen Jan 28 '21 at 13:09
  • `value || 0` makes no sense and isn't even legal Dart. – jamesdlin Jan 29 '21 at 09:22
  • sry that was a typo its double question mark – SWAG Assassin YT Jan 29 '21 at 10:22