Last active
January 9, 2023 20:40
-
-
Save muncman/67ca5cfeee480857c5e9075854d3e433 to your computer and use it in GitHub Desktop.
Some Dart Nullable Noodlings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() async { | |
oneWay(); | |
anotherWay(); | |
orThisWay(); | |
} | |
String? getNullable() { | |
if (DateTime.now().second % 2 == 0) { | |
return 'not null'; | |
} | |
return null; | |
} | |
void oneWay() { | |
String? nilMaybe = getNullable(); | |
final String notNil = (nilMaybe != null) ? nilMaybe : '(null)'; | |
print('Result: $notNil'); | |
} | |
void anotherWay() { | |
String? nilMaybe = getNullable(); | |
if (nilMaybe != null) { | |
processNonNull(nilMaybe); | |
} else { | |
print('...was null'); | |
} | |
} | |
void processNonNull(String cannotBeNull) { | |
print('Processed: $cannotBeNull'); | |
} | |
void orThisWay() { | |
String? nilMaybe = getNullable(); | |
processNonNull(nilMaybe ?? '(null)'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment