default_list_constructor
The default 'List' constructor isn't available when null safety is enabled.
Description
#The analyzer produces this diagnostic when it finds a use of the default
constructor for the class List
in code that has opted in to null safety.
Example
#Assuming the following code is opted in to null safety, it produces this
diagnostic because it uses the default List
constructor:
var l = List<int>();
Common fixes
#If no initial size is provided, then convert the code to use a list literal:
var l = <int>[];
If an initial size needs to be provided and there is a single reasonable
initial value for the elements, then use List.filled
:
var l = List.filled(3, 0);
If an initial size needs to be provided but each element needs to be
computed, then use List.generate
:
var l = List.generate(3, (i) => i);
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。