Last active
March 21, 2021 17:54
-
-
Save nstrelow/338245c6ada3f75690e2e24493387eb7 to your computer and use it in GitHub Desktop.
Nullable default parameter
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() { | |
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