Skip to content

Instantly share code, notes, and snippets.

@dotdoom
Created December 25, 2018 18:19
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 dotdoom/9c36dcd5936cdb7d96c002d4ba5b61c3 to your computer and use it in GitHub Desktop.
Save dotdoom/9c36dcd5936cdb7d96c002d4ba5b61c3 to your computer and use it in GitHub Desktop.
Default initializer vs constructor
class A {
String value;
A(this.value) { print(value); }
String toString() => value;
}
class B {
A a = A('Default');
B();
B.withANull() : this.a = null;
B.withAInitList() : this.a = A('Init list');
B.withAParam(this.a);
B.withAConstruct() { this.a = A('Construct'); }
}
void main() {
print('>>> B()');
print(B().a);
print('>>> B.withANull()');
print(B.withANull().a);
print('>>> B.withAInitList()');
print(B.withAInitList().a);
print('>>> B.withAParam()');
print(B.withAParam(A('Param')).a);
print('>>> B.withAConstruct()');
print(B.withAConstruct().a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment