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);
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.