Equivalent of if let and guard let of Swift in Dart
Asked Answered
B

5

28

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

Blida answered 28/1, 2021 at 12:44 Comment(0)
F
33

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 or for instance variables that aren't both final and private, so for those you would need to explicitly introduce a local reference.

As of Dart 3, you can use a null-check pattern to do the same without introducing that local variable to the outer scope:

if (possiblyReturnsNull() case var x?) {
  // All code within this block treats `x` as non-nullable.
}

// `x` does not exist.
Floriated answered 28/1, 2021 at 20:23 Comment(1)
the non-local thing explains, I was wondering why in some cases it works and some don't. ThanksBlida
T
8

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.
Twinned answered 23/4, 2021 at 2:32 Comment(0)
A
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]!)
}
//
Apulia answered 13/8, 2021 at 18:51 Comment(0)
A
-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

Alverson answered 20/4, 2023 at 13:29 Comment(0)
G
-7

To check null safety in Dart:

value ?? 0
Goal answered 28/1, 2021 at 12:56 Comment(3)
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.Blida
value || 0 makes no sense and isn't even legal Dart.Floriated
sry that was a typo its double question markGoal

© 2022 - 2024 — McMap. All rights reserved.