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>[];
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。