Skip to content

Instantly share code, notes, and snippets.

@passsy
Created December 29, 2019 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passsy/3c884a82f6937e90bc132626e63321b1 to your computer and use it in GitHub Desktop.
Save passsy/3c884a82f6937e90bc132626e63321b1 to your computer and use it in GitHub Desktop.
Comparison between ?? and a orDefault method
void main() {
final String name = "Adam";
print("=== ?? ===");
// does not execute defaultValue()
print(name ?? defaultValue());
print("=== orDefault ===");
// executes defaultValue()
print(name.orDefault(defaultValue()));
}
String defaultValue() {
print("execute defaultValue()");
return "Charles";
}
extension Ext<T> on T {
T orDefault(T defaultValue) {
if (this == null) return defaultValue;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment