So this is the code, which won't work due to "Case expressions must be constant"
void main() {
final id = 'someIdA';
switch (id) {
case ConstantsClass.a.id:
// do something
break;
case ConstantsClass.b.id:
// do something
break;
}
}
// exposing all the constants but grouping the constants in class by class fashion
class ConstantsClass {
static _A get a => _A();
static _B get b => _B();
}
// wrapping all the constants related to A
class _A {
final id = 'someIdA';
}
// wrapping all the constants related to B
class _B {
final id = 'someIdB';
}
And I have read posts about this "Case expressions must be constant" issue: 1 2
But I don't seem to find an answer from those posts.
And I also try to find another way to do use a class that contains only constant fields: What's the best practice to keep all the constants in Flutter? [closed] which also gives me not much hope...
And I think there's no way for me to use class with constant fields like ConstantsClass
in the code here, the best I can get is to put all the constant values in one class, which I think will be very messy...
so is there a better way?
edit1:
I have a lot of String literal values that I want to manage using class, like in the case that's discussed in Effective Dart
So I can't use enum, unless I need to convert every string literal to enum, which I'm not sure if it's possible...
Thanks!