I want to initialize a couple of final member variables using the same function. Unfortunately dart does not allow function calls within the initializer list of a const constructor:
int fun(int val) => val + 1;
class Foo {
final int a;
final int b;
final int c;
const Foo(int a, int b, int c)
: a = fun(a), <-- this won't compile because
b = fun(b), <-- the constructor
c = fun(c); <-- is const
}
I absolutely need the constructor to be a constant expression (in order to maintain compatability with existing third party library code). The only workaround I can think of is to repeatedly copy and paste the entire function body into the initializer list. I have already seen this antipattern used in some flutter libraries. Still I would rather avoid it. Does anybody know of a cleaner solution to the problem?