Created
November 7, 2021 11:13
-
-
Save tomaszpolanski/24b40720feaf42fd641150dda167f4d8 to your computer and use it in GitHub Desktop.
Not truly final fields
This file contains hidden or 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment