{%- comment %} WARNING: Do NOT EDIT this file directly. It is autogenerated by the script in pkg/analyzer/tool/diagnostics/generate.dart
in the sdk repository. {% endcomment -%}
This page lists diagnostic messages produced by the Dart analyzer, with details about what those messages mean and how you can fix your code. For more information about the analyzer, see Customizing static analysis.
This page uses the following terms.
A constant context is a region of code in which it isn‘t necessary to include the const
keyword because it’s implied by the fact that everything in that region is required to be a constant. The following locations are constant contexts:
Everything inside a list, map or set literal that's prefixed by the const
keyword. Example:
var l = const [/*constant context*/];
The arguments inside an invocation of a constant constructor. Example:
var p = const Point(/*constant context*/);
The initializer for a variable that's prefixed by the const
keyword. Example:
const v = /*constant context*/;
Annotations
The expression in a case clause. Example:
void f(int e) { switch (e) { case /*constant context*/: break; } }
The analyzer produces the following diagnostics for code that doesn't conform to the language specification or that might work in unexpected ways.
This literal contains both ‘Map’ and ‘Iterable’ spreads, which makes it impossible to determine whether the literal is a map or a set.
Because map and set literals use the same delimiters ({
and }
), the analyzer looks at the type arguments and the elements to determine which kind of literal you meant. When there are no type arguments and all of the elements are spread elements (which are allowed in both kinds of literals), then the analyzer uses the types of the expressions that are being spread. If all of the expressions have the type Iterable
, then it‘s a set literal; if they all have the type Map
, then it’s a map literal.
The analyzer produces this diagnostic when some of the expressions being spread have the type Iterable
and others have the type Map
, making it impossible for the analyzer to determine whether you are writing a map literal or a set literal.
The following code produces this diagnostic:
{% prettify dart %} union(Map<String, String> a, List b, Map<String, String> c) => [!{...a, ...b, ...c}!]; {% endprettify %}
The list b
can only be spread into a set, and the maps a
and c
can only be spread into a map, and the literal can't be both.
There are two common ways to fix this problem. The first is to remove all of the spread elements of one kind or another, so that the elements are consistent. In this case, that likely means removing the list and deciding what to do about the now unused parameter:
{% prettify dart %} union(Map<String, String> a, List b, Map<String, String> c) => {...a, ...c}; {% endprettify %}
The second fix is to change the elements of one kind into elements that are consistent with the other elements. For example, you can add the elements of the list as keys that map to themselves:
{% prettify dart %} union(Map<String, String> a, List b, Map<String, String> c) => {...a, for (String s in b) s: s, ...c}; {% endprettify %}
This literal must be either a map or a set, but the elements don't have enough information for type inference to work.
Because map and set literals use the same delimiters ({
and }
), the analyzer looks at the type arguments and the elements to determine which kind of literal you meant. When there are no type arguments and all of the elements are spread elements (which are allowed in both kinds of literals) then the analyzer uses the types of the expressions that are being spread. If all of the expressions have the type Iterable
, then it‘s a set literal; if they all have the type Map
, then it’s a map literal.
This diagnostic is produced when none of the expressions being spread have a type that allows the analyzer to decide whether you were writing a map literal or a set literal.
The following code produces this diagnostic:
{% prettify dart %} union(a, b) => [!{...a, ...b}!]; {% endprettify %}
The problem occurs because there are no type arguments, and there is no information about the type of either a
or b
.
There are three common ways to fix this problem. The first is to add type arguments to the literal. For example, if the literal is intended to be a map literal, you might write something like this:
{% prettify dart %} union(a, b) => <String, String>{...a, ...b}; {% endprettify %}
The second fix is to add type information so that the expressions have either the type Iterable
or the type Map
. You can add an explicit cast or, in this case, add types to the declarations of the two parameters:
{% prettify dart %} union(List a, List b) => {...a, ...b}; {% endprettify %}
The third fix is to add context information. In this case, that means adding a return type to the function:
{% prettify dart %} Set union(a, b) => {...a, ...b}; {% endprettify %}
In other cases, you might add a type somewhere else. For example, say the original code looks like this:
{% prettify dart %} union(a, b) { var x = {...a, ...b}; return x; } {% endprettify %}
You might add a type annotation on x
, like this:
{% prettify dart %} union(a, b) { Map<String, String> x = {...a, ...b}; return x; } {% endprettify %}
The argument type ‘{0}’ can't be assigned to the parameter type ‘{1}’.
The analyzer produces this diagnostic when the static type of an argument can't be assigned to the static type of the corresponding parameter.
The following code produces this diagnostic:
{% prettify dart %} int f(int x) => x; num g(num y) => f([!y!]); {% endprettify %}
If possible, rewrite the code so that the static type is assignable. In the example above you might be able to change the type of the parameter y
:
{% prettify dart %} int f(int x) => x; int g(int y) => f(y); {% endprettify %}
If that fix isn‘t possible, then add code to handle the case where the argument value isn’t the required type. One approach is to coerce other types to the required type:
{% prettify dart %} int f(int x) => x; num g(num y) => f(y.floor()); {% endprettify %}
Another approach is to add explicit type tests and fallback code:
{% prettify dart %} int f(int x) => x; num g(num y) => f(y is int ? y : 0); {% endprettify %}
If you believe that the runtime type of the argument will always be the same as the static type of the parameter, and you‘re willing to risk having an exception thrown at runtime if you’re wrong, then add an explicit cast:
{% prettify dart %} int f(int x) => x; num g(num y) => f(y as int); {% endprettify %}
Const variables must be initialized with a constant value.
The analyzer produces this diagnostic when a value that isn‘t statically known to be a constant is assigned to a variable that’s declared to be a ‘const’ variable.
The following code produces this diagnostic because x
isn't declared to be const
:
{% prettify dart %} var x = 0; const y = [!x!]; {% endprettify %}
If the value being assigned can be declared to be const
, then change the declaration:
{% prettify dart %} const x = 0; const y = x; {% endprettify %}
If the value can't be declared to be const
, then remove the const
modifier from the variable, possibly using final
in its place:
{% prettify dart %} var x = 0; final y = x; {% endprettify %}
‘{0}’ is deprecated and shouldn't be used.
The analyzer produces this diagnostic when a deprecated library or class member is used in a different package.
If the method m
in the class C
is annotated with @deprecated
, then the following code produces this diagnostic:
{% prettify dart %} void f(C c) { c.!m!; } {% endprettify %}
The documentation for declarations that are annotated with @deprecated
should indicate what code to use in place of the deprecated code.
‘{0}’ is deprecated and shouldn't be used.
The analyzer produces this diagnostic when a deprecated library member or class member is used in the same package in which it's declared.
The following code produces this diagnostic:
{% prettify dart %} @deprecated var x = 0; var y = [!x!]; {% endprettify %}
The fix depends on what's been deprecated and what the replacement is. The documentation for deprecated declarations should indicate what code to use in place of the deprecated code.
Two keys in a constant map literal can't be equal.
The analyzer produces this diagnostic when a key in a constant map is the same as a previous key in the same map. If two keys are the same, then the second value would overwrite the first value, which makes having both pairs pointless.
The following code produces this diagnostic:
{% prettify dart %} const map = <int, String>{1: ‘a’, 2: ‘b’, [!1!]: ‘c’, 4: ‘d’}; {% endprettify %}
If one of the keys was supposed to be different, then replace it:
{% prettify dart %} const map = <int, String>{1: ‘a’, 2: ‘b’, 3: ‘c’, 4: ‘d’}; {% endprettify %}
Otherwise, remove the key/value pair that isn't intended to be in the map:
{% prettify dart %} const map = <int, String>{1: ‘a’, 2: ‘b’, 4: ‘d’}; {% endprettify %}
Expressions can't be used in a map literal.
The analyzer produces this diagnostic when the analyzer finds an expression, rather than a map entry, in what appears to be a map literal.
The following code generates this diagnostic:
{% prettify dart %} var map = <String, int>{‘a’: 0, ‘b’: 1, [!‘c’!]}; {% endprettify %}
If the expression is intended to compute either a key or a value in an entry, fix the issue by replacing the expression with the key or the value. For example:
{% prettify dart %} var map = <String, int>{‘a’: 0, ‘b’: 1, ‘c’: 2}; {% endprettify %}
Only const constructors can have the @literal
annotation.
The meaning of the @literal
annotation is only defined when it's applied to a const constructor.
The following code produces this diagnostic:
{% prettify dart %} [!@literal!] var x; {% endprettify %}
Remove the annotation:
{% prettify dart %} var x; {% endprettify %}
‘{0}’ isn't a function.
The analyzer produces this diagnostic when it finds a function invocation, but the name of the function being invoked is defined to be something other than a function.
The following code produces this diagnostic:
{% prettify dart %} typedef Binary = int Function(int, int);
int f() { return [!Binary!](1, 2); } {% endprettify %}
Replace the name with the name of a function.
This function has a return type of ‘{0}’, but doesn't end with a return statement.
Any function or method that doesn’t end with either an explicit return or a throw implicitly returns null
. This is rarely the desired behavior. The analyzer produces this diagnostic when it finds an implicit return.
The following code produces this diagnostic:
{% prettify dart %} [!int!] f(int x) { if (x < 0) { return 0; } } {% endprettify %}
Add a return statement that makes the return value explicit, even if null
is the appropriate value.
The values in a const list literal must be constants.
The analyzer produces this diagnostic when an element in a constant list literal isn‘t a constant value. The list literal can be constant either explicitly (because it’s prefixed by the const
keyword) or implicitly (because it appears in a constant context).
The following code produces this diagnostic because x
isn't a constant, even though it appears in an implicitly constant list literal:
{% prettify dart %} int x = 2; const y = [0, 1, [!x!]]; {% endprettify %}
If the list needs to be a constant list, then convert the element to be a constant. In the example above, you might add the const
keyword to the declaration of x
:
{% prettify dart %} const int x = 2; const y = [0, 1, x]; {% endprettify %}
If the expression can‘t be made a constant, then the list can’t be a constant either, so you must change the code so that the list isn't a constant. In the example above this means removing the const
keyword from the declaration of y
:
{% prettify dart %} int x = 2; var y = [0, 1, x]; {% endprettify %}
The name ‘{0}’ isn‘t a type so it can’t be used as a type argument.
The analyzer produces this diagnostic when an identifier that isn't a type is used as a type argument.
The following code produces this diagnostic because x
is a variable, not a type:
{% prettify dart %} var x = 0; List<[!x!]> xList = []; {% endprettify %}
Change the type argument to be a type:
{% prettify dart %} var x = 0; List xList = []; {% endprettify %}
{0} isn't a type.
The analyzer produces this diagnostic when a name is used as a type but declared to be something other than a type.
The following code produces this diagnostic because f
is a function:
{% prettify dart %} f() {} main() { [!f!] v = null; } {% endprettify %}
Replace the name with the name of a type.
Spread elements in list or set literals must implement ‘Iterable’.
The analyzer produces this diagnostic when the static type of the expression of a spread element that appears in either a list literal or a set literal doesn't implement the type Iterable
.
The following code generates this diagnostic:
{% prettify dart %} var m = <String, int>{‘a’: 0, ‘b’: 1}; var s = {...[!m!]}; {% endprettify %}
The most common fix is to replace the expression with one that produces an iterable object:
{% prettify dart %} var m = <String, int>{‘a’: 0, ‘b’: 1}; var s = {...m.keys}; {% endprettify %}
The name ‘{0}’ isn‘t a type and can’t be used in a redirected constructor.
One way to implement a factory constructor is to redirect to another constructor by referencing the name of the constructor. The analyzer produces this diagnostic when the redirect is to something other than a constructor.
The following code produces this diagnostic because f
is a function:
{% prettify dart %} C f() {}
class C { factory C() = [!f!]; } {% endprettify %}
If the constructor isn't defined, then either define it or replace it with a constructor that is defined.
If the constructor is defined but the class that defines it isn't visible, then you probably need to add an import.
If you‘re trying to return the value returned by a function, then rewrite the constructor to return the value from the constructor’s body:
{% prettify dart %} C f() {}
class C { factory C() => f(); } {% endprettify %}
Set literals weren't supported until version 2.2, but this code is required to be able to run on earlier versions.
The analyzer produces this diagnostic when a set literal is found in code that has an SDK constraint whose lower bound is less than 2.2. Set literals weren‘t supported in earlier versions, so this code won’t be able to run against earlier versions of the SDK.
In a package that defines the SDK constraint (in the pubspec.yaml file), with a lower bound of less than 2.2. For example:
environment: sdk: '>=2.1.0 <2.4.0'
The following code generates this diagnostic:
{% prettify dart %} var s = [!{}!]; {% endprettify %}
If you don't need to support older versions of the SDK, then you can increase the SDK constraint to allow the syntax to be used:
environment: sdk: '>=2.2.0 <2.4.0'
If you do need to support older versions of the SDK, then replace the set literal with code that creates the set without the use of a literal:
{% prettify dart %} var s = new Set(); {% endprettify %}
‘{0}’ doesn't extend ‘{1}’.
The analyzer produces this diagnostic when a type argument isn't the same as or a subclass of the bounds of the corresponding type parameter.
The following code produces this diagnostic:
{% prettify dart %} class A {}
var a = A<[!String!]>(); {% endprettify %}
Change the type argument to be a subclass of the bounds:
{% prettify dart %} class A {}
var a = A(); {% endprettify %}
Undefined class ‘{0}’.
The analyzer produces this diagnostic when it encounters an identifier that appears to be the name of a class but either isn‘t defined or isn’t visible in the scope in which it's being referenced.
The following code produces this diagnostic:
{% prettify dart %} class Point {}
void main() { [!Piont!] p; } {% endprettify %}
If the identifier isn't defined, then either define it or replace it with the name of a class that is defined. The example above can be corrected by fixing the spelling of the class:
{% prettify dart %} class Point {}
void main() { Point p; } {% endprettify %}
If the class is defined but isn't visible, then you probably need to add an import.
The function ‘{0}’ isn't defined.
The analyzer produces this diagnostic when it encounters an identifier that appears to be the name of a function but either isn‘t defined or isn’t visible in the scope in which it's being referenced.
The following code produces this diagnostic:
{% prettify dart %} List empty() => [];
void main() { print(!emty!); } {% endprettify %}
If the identifier isn't defined, then either define it or replace it with the name of a function that is defined. The example above can be corrected by fixing the spelling of the function:
{% prettify dart %} List empty() => [];
void main() { print(empty()); } {% endprettify %}
If the function is defined but isn't visible, then you probably need to add an import or re-arrange your code to make the function visible.
The getter ‘{0}’ isn't defined for the class ‘{1}’.
The analyzer produces this diagnostic when it encounters an identifier that appears to be the name of a getter but either isn‘t defined or isn’t visible in the scope in which it's being referenced.
The following code produces this diagnostic:
{% prettify dart %} int f(String s) => s.[!len!]; {% endprettify %}
If the identifier isn't defined, then either define it or replace it with the name of a getter that is defined. The example above can be corrected by fixing the spelling of the getter:
{% prettify dart %} int f(String s) => s.length; {% endprettify %}
Undefined name ‘{0}’.
The analyzer produces this diagnostic when it encounters an identifier that either isn‘t defined or isn’t visible in the scope in which it's being referenced.
The following code produces this diagnostic:
{% prettify dart %} int min(int left, int right) => left <= [!rihgt!] ? left : right; {% endprettify %}
If the identifier isn't defined, then either define it or replace it with an identifier that is defined. The example above can be corrected by fixing the spelling of the variable:
{% prettify dart %} int min(int left, int right) => left <= right ? left : right; {% endprettify %}
If the identifier is defined but isn't visible, then you probably need to add an import or re-arrange your code to make the identifier visible.
The method ‘{0}’ isn't defined for the class ‘{1}’.
The analyzer produces this diagnostic when it encounters an identifier that appears to be the name of a method but either isn‘t defined or isn’t visible in the scope in which it's being referenced.
The following code produces this diagnostic:
{% prettify dart %} int f(List l) => l.!removeMiddle!; {% endprettify %}
If the identifier isn't defined, then either define it or replace it with the name of a method that is defined. The example above can be corrected by fixing the spelling of the method:
{% prettify dart %} int f(List l) => l.removeLast(); {% endprettify %}
The named parameter ‘{0}’ isn't defined.
The analyzer produces this diagnostic when a method or function invocation has a named argument, but the method or function being invoked doesn’t define a parameter with the same name.
The following code produces this diagnostic:
{% prettify dart %} class C { m({int b}) {} }
void f(C c) { c.m([!a!]: 1); } {% endprettify %}
If the argument name is mistyped, then replace it with the correct name. The example above can be fixed by changing a
to b
:
{% prettify dart %} class C { m({int b}) {} }
void f(C c) { c.m(b: 1); } {% endprettify %}
If a subclass adds a parameter with the name in question, then cast the target to the subclass:
{% prettify dart %} class C { m({int b}) {} }
class D extends C { m({int a, int b}) {} }
void f(C c) { (c as D).m(a: 1); } {% endprettify %}
If the parameter should be added to the function, then add it:
{% prettify dart %} class C { m({int a, int b}) {} }
void f(C c) { c.m(a: 1); } {% endprettify %}
The setter ‘{0}’ isn't defined for the class ‘{1}’.
The analyzer produces this diagnostic when it encounters an identifier that appears to be the name of a setter but either isn‘t defined or isn’t visible in the scope in which the identifier is being referenced.
The following code produces this diagnostic:
{% prettify dart %} class Point { int x; int y; Point(this.x, this.y); void shiftBy(Point other) { this.x += other.x; this.[!z!] += other.y; } } {% endprettify %}
If the identifier isn't defined, then either define it or replace it with the name of a setter that is defined. The example above can be corrected by fixing the spelling of the setter:
{% prettify dart %} class Point { int x; int y; Point(this.x, this.y); void shiftBy(Point other) { this.x += other.x; this.y += other.y; } } {% endprettify %}
The declaration ‘{0}’ isn't referenced.
The analyzer produces this diagnostic when a private class, enum, mixin, typedef, top level variable, top level function, or method is declared but never referenced.
Assuming that no code in the library references _C
, the following code produces this diagnostic:
{% prettify dart %} class [!_C!] {} {% endprettify %}
If the declaration isn't needed, then remove it.
If the declaration was intended to be used, then add the missing code.
The value of the field ‘{0}’ isn't used.
The analyzer produces this diagnostic when a private field is declared but never read, even if it's written in one or more places.
The following code produces this diagnostic:
{% prettify dart %} class Point { int [!_x!]; } {% endprettify %}
If the field isn't needed, then remove it.
If the field was intended to be used, then add the missing code.
Unused import: ‘{0}’.
The analyzer produces this diagnostic when an import isn't needed because none of the names that are imported are referenced within the importing library.
The following code produces this diagnostic:
{% prettify dart %} import [!‘dart:async’!];
void main() {} {% endprettify %}
If the import isn't needed, then remove it.
If some of the imported names are intended to be used, then add the missing code.
The value of the local variable ‘{0}’ isn't used.
The analyzer produces this diagnostic when a local variable is declared but never read, even if it's written in one or more places.
The following code produces this diagnostic:
{% prettify dart %} void main() { int [!count!] = 0; } {% endprettify %}
If the variable isn't needed, then remove it.
If the variable was intended to be used, then add the missing code.
Target of URI doesn't exist: ‘{0}’.
The analyzer produces this diagnostic when an import, export, or part directive is found where the URI refers to a file that doesn't exist.
If the file lib.dart
doesn't exist, the following code produces this diagnostic:
{% prettify dart %} import [!‘lib.dart’!]; {% endprettify %}
If the URI was mistyped or invalid, then correct the URI.
If the URI is correct, then create the file.
Target of URI hasn't been generated: ‘{0}’.
The analyzer produces this diagnostic when an import, export, or part directive is found where the URI refers to a file that doesn‘t exist and the name of the file ends with a pattern that’s commonly produced by code generators, such as one of the following:
.g.dart
.pb.dart
.pbenum.dart
.pbserver.dart
.pbjson.dart
.template.dart
If the file lib.g.dart
doesn't exist, the following code produces this diagnostic:
{% prettify dart %} import [!‘lib.g.dart’!]; {% endprettify %}
If the file is a generated file, then run the generator that generates the file.
If the file isn't a generated file, then check the spelling of the URI or create the file.