Skip to content

Instantly share code, notes, and snippets.

@tomaszpolanski
Last active November 7, 2021 10:33
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 tomaszpolanski/f68c07e6de1c63cda4ccb842af3264eb to your computer and use it in GitHub Desktop.
Save tomaszpolanski/f68c07e6de1c63cda4ccb842af3264eb to your computer and use it in GitHub Desktop.
Issue With dart null safety 1
class A {
A(this.text);
final String? text; // Final field
}
class B implements A {
bool _first = true;
@override
// Not final anymore
String? get text {
if (_first) {
_first = false;
// On the first call return null
return 'Some string';
} else {
// On the next calls return null
return null;
}
}
}
void main() {
final A example = B();
print(example.text); // Prints 'Some string'
print(example.text); // Prints null
}
void main() {
final A example = B();
if (example.text != null) {
print(example.text!.length);
}
}
final String? text;
@override
Widget build(BuildContext context) {
return Column(
children: [
if (text != null) Text(text!), // Won't compile without `!` in `text!`
],
);
}
final String? text;
@override
Widget build(BuildContext context) {
final _text = text;
return Column(
children: [
if (_text != null) Text(_text), // The `!` is not longer needed
],
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment