Description
Inside a const constructor, a nested const constructor invocation passed
through a redirecting this(...) initializer is reported as:
Invalid constant value.
Adding an explicit const before the nested constructor invocation fixes the
error.
This is surprising because the surrounding constructor is declared const,
and the this(...) invocation must itself produce a constant object.
Minimal reproduction
class OrderViewPrintConfiguration {
const OrderViewPrintConfiguration.initial();
}
class OrderViewConfiguration {
final bool ignoreUndefinedFloorPlate;
final OrderViewPrintConfiguration orderViewPrintConfiguration;
const OrderViewConfiguration({
required this.ignoreUndefinedFloorPlate,
required this.orderViewPrintConfiguration,
});
const OrderViewConfiguration.initial()
: this(
ignoreUndefinedFloorPlate: false,
orderViewPrintConfiguration:
OrderViewPrintConfiguration.initial(),
);
}
The analyzer reports:
Workaround
Explicitly adding const fixes the error:
const OrderViewConfiguration.initial()
: this(
ignoreUndefinedFloorPlate: false,
orderViewPrintConfiguration:
const OrderViewPrintConfiguration.initial(),
);
Expected behavior
One of the following would be preferable:
-
Treat the arguments of the redirecting this(...) invocation in a const
constructor as a const context, allowing the nested const keyword to be
omitted.
-
If the current behavior is intentional, produce a more specific diagnostic
and quick-fix:
This constructor invocation must be constant. Try adding const.
The current Invalid constant value diagnostic does not make it clear that
the nested constructor is const-capable and only requires an explicit keyword.
Actual behavior
The invocation is rejected unless const is written explicitly.
Description
Inside a
constconstructor, a nested const constructor invocation passedthrough a redirecting
this(...)initializer is reported as:Adding an explicit
constbefore the nested constructor invocation fixes theerror.
This is surprising because the surrounding constructor is declared
const,and the
this(...)invocation must itself produce a constant object.Minimal reproduction
The analyzer reports:
Workaround
Explicitly adding
constfixes the error:Expected behavior
One of the following would be preferable:
Treat the arguments of the redirecting
this(...)invocation in a constconstructor as a const context, allowing the nested
constkeyword to beomitted.
If the current behavior is intentional, produce a more specific diagnostic
and quick-fix:
The current
Invalid constant valuediagnostic does not make it clear thatthe nested constructor is const-capable and only requires an explicit keyword.
Actual behavior
The invocation is rejected unless
constis written explicitly.