const_with_type_parameters
A constant constructor tearoff can't use a type parameter as a type argument.
A constant creation can't use a type parameter as a type argument.
A constant function tearoff can't use a type parameter as a type argument.
Description
#The analyzer produces this diagnostic when a type parameter is used as a type argument in a const
invocation of a constructor. This isn't allowed because the value of the type parameter (the actual type that will be used at runtime) can't be known at compile time.
Example
#The following code produces this diagnostic because the type parameter T
is being used as a type argument when creating a constant:
class C<T> {
const C();
}
C<T> newC<T>() => const C<T>();
Common fixes
#If the type that will be used for the type parameter can be known at compile time, then remove the use of the type parameter:
class C<T> {
const C();
}
C<int> newC() => const C<int>();
If the type that will be used for the type parameter can't be known until runtime, then remove the keyword const
:
class C<T> {
const C();
}
C<T> newC<T>() => C<T>();
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.