Skip to content

Instantly share code, notes, and snippets.

@muncman
Last active January 9, 2023 20:40
Show Gist options
  • Save muncman/67ca5cfeee480857c5e9075854d3e433 to your computer and use it in GitHub Desktop.
Save muncman/67ca5cfeee480857c5e9075854d3e433 to your computer and use it in GitHub Desktop.
Some Dart Nullable Noodlings
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