Last active
November 7, 2021 10:33
-
-
Save tomaszpolanski/f68c07e6de1c63cda4ccb842af3264eb to your computer and use it in GitHub Desktop.
Issue With dart null safety 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final A example = B(); | |
if (example.text != null) { | |
print(example.text!.length); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final String? text; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
if (text != null) Text(text!), // Won't compile without `!` in `text!` | |
], | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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