Skip to content

Instantly share code, notes, and snippets.

@tomaszpolanski
Created November 7, 2021 11:13
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/24b40720feaf42fd641150dda167f4d8 to your computer and use it in GitHub Desktop.
Save tomaszpolanski/24b40720feaf42fd641150dda167f4d8 to your computer and use it in GitHub Desktop.
Not truly final fields
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