Skip to content

Instantly share code, notes, and snippets.

@creativecreatorormaybenot
Created July 24, 2020 12: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 creativecreatorormaybenot/a5b1ae1e3cbc2a389b713f49fd910a0f to your computer and use it in GitHub Desktop.
Save creativecreatorormaybenot/a5b1ae1e3cbc2a389b713f49fd910a0f to your computer and use it in GitHub Desktop.
late in constructors
void main() {
// Construct the object first.
final baz = Baz(calculate('assignmentInConstructor'));
// Call access to access the late variables and initialize them if they are lazy.
baz.access();
}
class Baz {
Baz(
// Implicit assignment by the caller.
this.assignmentInConstructor,
) : assignmentInInitializerList = calculate('assignmentInInitializerList') {
assignmentInConstructorBody = calculate('assignmentInConstructorBody');
}
late final int
assignmentInConstructor; // I expect this to be lazily initialized.
late final int
assignmentInInitializerList; // I expect this to also be lazily initialized.
late final int
assignmentInConstructorBody; // I expect this to not be lazily initialized.
void access() {
print('access');
print('$assignmentInConstructor'
'$assignmentInInitializerList'
'$assignmentInConstructorBody');
}
}
int calculate(String message) {
print('calcuate $message');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment