Skip to content

Instantly share code, notes, and snippets.

@timsneath
Created October 22, 2020 17:22
Show Gist options
  • Save timsneath/1d5c7ec2ca86db4b81082e0691787bab to your computer and use it in GitHub Desktop.
Save timsneath/1d5c7ec2ca86db4b81082e0691787bab to your computer and use it in GitHub Desktop.
class StatusLine extends StatelessWidget {
final Status status;
StatusLine({this.status: Status.failed});
@override
Widget build(BuildContext context) {
// This local variable is non-nullable, but not initialized.
String statusText;
if (status == Status.ok) {
statusText = 'Update succeeded';
} else {
statusText = 'Update failed';
}
// By this point, Dart knows statusText isn't null, as we've
// assigned a value in both the if- and else-statements. Thus
// we can pass it to the Text widget -- which doesn't accept
// nullable Strings -- without getting an error.
return Text(statusText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment