Skip to content

Instantly share code, notes, and snippets.

@nstrelow
Last active March 21, 2021 17:54
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 nstrelow/338245c6ada3f75690e2e24493387eb7 to your computer and use it in GitHub Desktop.
Save nstrelow/338245c6ada3f75690e2e24493387eb7 to your computer and use it in GitHub Desktop.
Nullable default parameter
void main() {
String? nullableButHasValue = 'hello';
String? nullableAndIsNull;
func1(password: nullableButHasValue); //<-- Works, because the check registers, that is has a value
//func1(password: nullableAndIsNull); //<-- Errors, bc param is non-nullable and has no value.
//func1(param: null); //<-- Errors, bc param is non-nullable
func2(); // Works as expected
func2(password: null); // password is set to null, instead of 'test-nullable'
bestFunc(); // Uses default
bestFunc(password: null); // Uses default
bestFunc(password: 'My password'); // Uses parameter
}
void func1({String password = 'test'}) {
print(password);
}
void func2({String? password = 'test-nullable'}) {
print(password);
}
void bestFunc({String? password}) {
print(password ?? 'real default value, which is not overwritten by null');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment