invalid_ type_ argument_ in_ const_ literal
Constant list literals can't use a type parameter in a type argument, such as '{0}'.
Constant map literals can't use a type parameter in a type argument, such as '{0}'.
Constant set literals can't use a type parameter in a type argument, such as '{0}'.
Description
#
The analyzer produces this diagnostic when a type parameter is used in a
type argument in a list, map, or set literal that is prefixed by const.
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.
Examples
#
The following code produces this diagnostic because the type parameter T
is being used as a type argument when creating a constant list:
List<T> newList<T>() => const <T>[];
The following code produces this diagnostic because the type parameter T
is being used as a type argument when creating a constant map:
Map<String, T> newSet<T>() => const <String, T>{};
The following code produces this diagnostic because the type parameter T
is being used as a type argument when creating a constant set:
Set<T> newSet<T>() => const <T>{};
Common fixes
#If the type that will be used for the type parameter can be known at compile time, then remove the type parameter:
List<int> newList() => const <int>[];
If the type that will be used for the type parameter can't be known until
runtime, then remove the keyword const:
List<T> newList<T>() => <T>[];
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.