Don’t be always late…

enum Some { option1, option2 }

final someCondition = Some.option2;

void main() {
late final String x;

switch (someCondition) {
case Some.option1:
x = 'a';
break;
case Some.option2:
x = 'b';
break;
}

print(x);
}
enum Some { option1, option2 }

final someCondition = Some.option2;

void main() {
final String x;

switch (someCondition) {
case Some.option1:
x = 'a';
break;
case Some.option2:
x = 'b';
break;
}

print(x);
}
enum Some { option1, option2 }

final someCondition = Some.option2;

void main() {
late final String x;

switch (someCondition) {
case Some.option1:
x = 'a';
break;
}

print(x);
}
Uncaught Error: LateInitializationError: Local 'x' has not been initialized.
enum Some { option1, option2 }

final someCondition = Some.option2;

void main() {
final String x;

switch (someCondition) {
case Some.option1:
x = 'a';
break;
}

print(x);
}
Error: Final variable 'x' must be assigned before it can be used. print(x); ^ Error: Compilation failed.

--

--

Head of Applications @ Mews

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store