I am developing a tool that generates dart code and I am facing this runtime error when executing a program having a circular dependency.
file.dart:xx:xx: Error: Constant evaluation error:
this.b = const B(),,
^
file:xx:xx: Context: Constant expression depends on itself.
this. A = const A(),
Here is a simplification of the program I am executing:
class A {
final B b;
const A({
this.b = const B(),
});
}
class B {
final A a;
const B({
this.a = const A(),
});
}
As you can see there is a circular dependency between A and B.
I tried dropping the const keyword as:
class A {
final B b;
A({
this.b = B(),
});
}
class B {
final A a;
B({
this.a = A(),
});
}
But instead, I am getting a compile time error:
The default value of an optional parameter must be constant. dart(non_constant_default_value)
Do you have an idea how to handle this type of issue? Or is there an equivalent of forwardref in Dart?
Edit
At the current state of dart, it is not possible to avoid this circular dependency. I ended restricting this case. If the input has a circular dependency, it is normal that the output will have it.