Skip to content

Instantly share code, notes, and snippets.

@misterfourtytwo
Created April 15, 2021 15:35
Show Gist options
  • Save misterfourtytwo/7278f610e8a43638a8c17ebc7abe78ed to your computer and use it in GitHub Desktop.
Save misterfourtytwo/7278f610e8a43638a8c17ebc7abe78ed to your computer and use it in GitHub Desktop.
constant class field not const
const strings = const Strings();
class Strings {
const Strings();
final Feature1 feature1 = const Feature1();
// final Feature2 feature2 = const Feature2();
}
class Feature1 {
const Feature1({
this.text1 = 'x',
this.text2 = 'y',
});
final String text1;
final String text2;
}
void main() {
final t = 'x';
switch (t){
case strings.feature1.text1:
print('t is x');
break;
default:
print('t is not x');
}
}
@misterfourtytwo
Copy link
Author

misterfourtytwo commented Apr 19, 2021

so, const constructors will create defined compile-time literals, BUT THE LINKS TO THOS LITERALS IN THE FIELDS ARE NOT FIXED, SO EVENT IF YOU USE CONST CONSTRUCTOR, POINTERS TO THE FIELD LITERALS ARE DEFINED ON RUN-TIME, THUS FIELDS ARE NOT CONST.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment