Fixing common type problems

If you’re having problems with type checks, this page can help. To learn more, read about Dart’s type system, and see these other resources.

Help us improve this page! If you encounter a warning or error that isn’t listed here, please file an issue by clicking the bug icon at the top right. Include the warning or error message and, if possible, the code for both a small reproducible case and its correct equivalent.

Troubleshooting

Am I really using type-safe Dart?

If you’re not seeing expected errors or warnings, make sure that you’re using the latest version of Dart.

Alternatively, try adding the following code to a file:

  1. bool b = [0][0];

With type-safe Dart, the analyzer produces the following error:

  1. error A value of type 'int' can't be assigned to a variable of type 'bool' • invalid_assignment

Static errors and warnings

This section shows how to fix some of the errors and warnings you might see from the analyzer or an IDE.

Static analysis can’t catch all errors. For help fixing errors that appear only at runtime, see Runtime errors.

Undefined member

  1. error The <member> '...' isn't defined for the type '...' • undefined_<member>

These errors can appear under the following conditions:

  • A variable is statically known to be some supertype, but the code assumes a subtype.
  • A generic class has a bounded type parameter, but an instance creation expression of the class omits the type argument.

Example 1: A variable is statically known to be some supertype, but the code assumes a subtype

In the following code, the analyzer complains that context2D is undefined:

  1. var canvas = querySelector('canvas');
  2. canvas.context2D.lineTo(x, y);
  1. error The getter 'context2D' isn't defined for the type 'Element' • undefined_getter

Fix: Replace the definition of the member with an explicit type declaration or a downcast

The querySelector() method statically returns an Element, but the code assumes it returns the subtype CanvasElement where context2D is defined. The canvas field is declared as var which, in Dart 1.x versions without strong mode, types it as dynamic and silences all errors. Dart infers canvas to be an Element.

You can fix this error with an explicit type declaration:

  1. CanvasElement canvas = querySelector('canvas');
  2. canvas.context2D.lineTo(x, y);

The code above passes static analysis when implicit casts are permitted.

You can also use an explicit downcast:

  1. var canvas = querySelector('canvas') as CanvasElement;
  2. canvas.context2D.lineTo(x, y);

Otherwise, use dynamic in situations where you cannot use a single type:

  1. dynamic canvasOrImg = querySelector('canvas, img');
  2. var width = canvasOrImg.width;

Example 2: Omitted type parameters default to their type bounds

Consider the following generic class with a bounded type parameter that extends Iterable:

  1. class C<T extends Iterable> {
  2. final T collection;
  3. C(this.collection);
  4. }

The following code creates a new instance of this class (omitting the type argument) and accesses its collection member:

  1. var c = C(Iterable.empty()).collection;
  2. c.add(2);
  1. error The method 'add' isn't defined for the type 'Iterable'. • lib/bounded/instantiate_to_bound.dart:7:5 • undefined_method

While the List type has an add() method, Iterable does not.

Fix: Specify type arguments or fix downstream errors

In Dart 1.x, when a generic class is instantiated without explicit type arguments, dynamic is assumed. That is why, in the code excerpt above, c is of type dynamic and no error is reported for c.add().

In Dart 2, when a generic class is instantiated without explicit type arguments, each type parameter defaults to its type bound (Iterable in this example) if one is explicitly given, or dynamic otherwise.

You need to approach fixing such errors on a case-by-case basis. It helps to have a good understanding of the original design intent.

Explicitly passing type arguments is an effective way to help identify type errors. For example, if you change the code to specify List as a type argument, the analyzer can detect the type mismatch in the constructor argument. Fix the error by providing a constructor argument of the appropriate type:

  1. var c = C<List>([]).collection;
  2. c.add(2);

Invalid method override

  1. error '...' isn't a valid override of '...' • invalid_override

These errors typically occur when a subclass tightens up a method’s parameter types by specifying a subclass of the original class.

Note: This issue can also occur when a generic subclass neglects to specify a type. For more information, see Missing type arguments.

Example

In the following example, the parameters to the add() method are of type int, a subtype of num, which is the parameter type used in the parent class.

  1. abstract class NumberAdder {
  2. num add(num a, num b);
  3. }
  4. class MyAdder extends NumberAdder {
  5. int add(int a, int b) => a + b;
  6. }
  1. error 'MyAdder.add' ('int Function(int, int)') isn't a valid override of 'NumberAdder.add' ('num Function(num, num)') • invalid_override

Consider the following scenario where floating point values are passed to an MyAdder:

  1. NumberAdder adder = MyAdder();
  2. adder.add(1.2, 3.4);

If the override were allowed, the code would raise an error at runtime.

Fix: Widen the method’s parameter types

The subclass’s method should accept every object that the superclass’s method takes.

Fix the example by widening the types in the subclass:

  1. abstract class NumberAdder {
  2. num add(num a, num b);
  3. }
  4. class MyAdder extends NumberAdder {
  5. num add(num a, num b) => a + b;
  6. }

For more information, see Use proper input parameter types when overriding methods.

Note: If you have a valid reason to use a subtype, you can use the covariant keyword.


Missing type arguments

  1. error '...' isn't a valid override of '...' • invalid_override

Example

In the following example, Subclass extends Superclass<T> but doesn’t specify a type argument. The analyzer infers Subclass<dynamic>, which results in an invalid override error on method(int).

  1. class Superclass<T> {
  2. void method(T t) { ... }
  3. }
  4. class Subclass extends Superclass {
  5. void method(int i) { ... }
  6. }
  1. error 'Subclass.method' ('void Function(int)') isn't a valid override of 'Superclass.method' ('void Function(dynamic)') • invalid_override

Fix: Specify type arguments for the generic subclass

When a generic subclass neglects to specify a type argument, the analyzer infers the dynamic type. This is likely to cause errors.

You can fix the example by specifying the type on the subclass:

  1. class Superclass<T> {
  2. void method(T t) { ... }
  3. }
  4. class Subclass extends Superclass<int> {
  5. void method(int i) { ... }
  6. }

Unexpected collection element type

  1. error A value of type '...' can't be assigned to a variable of type '...' • invalid_assignment

This sometimes happens when you create a simple dynamic collection and the analyzer infers the type in a way you didn’t expect. When you later add values of a different type, the analyzer reports an issue.

Example

The following code initializes a map with several (String, int) pairs. The analyzer infers that map to be of type <String, int> but the code seems to assume either <String, dynamic> or <String, num>. When the code adds a (String, float) pair, the analyzer complains:

  1. // Inferred as Map<String, int>
  2. var map = {'a': 1, 'b': 2, 'c': 3};
  3. map['d'] = 1.5; // a double is not an int
  1. error A value of type 'double' can't be assigned to a variable of type 'int' • invalid_assignment

Fix: Specify the type explicitly

The example can be fixed by explicitly defining the map’s type to be <String, num>.

  1. var map = <String, num>{'a': 1, 'b': 2, 'c': 3};
  2. map['d'] = 1.5;

Alternatively, if you want this map to accept any value, specify the type as <String, dynamic>.


Constructor initialization list super() call

  1. error The super call must be last in an initializer list (see https://goo.gl/EY6hDP): '...' • invalid_super_invocation

This error occurs when the super() call is not last in a constructor’s initialization list.

Example

  1. HoneyBadger(Eats food, String name)
  2. : super(food),
  3. _name = name { ... }
  1. error The super call must be last in an initializer list (see https://goo.gl/EY6hDP): 'super(food)' • invalid_super_invocation

Fix: Put the super() call last

The compiler can generate simpler code if it relies on the super() call appearing last.

Fix this error by moving the super() call:

  1. HoneyBadger(Eats food, String name)
  2. : _name = name,
  3. super(food) { ... }

The function expression type … isn’t of type …

  1. error The function expression type '...' isn't of type '...'. This means its parameter or return type doesn't match what is expected. Consider changing parameter type(s) or the returned type(s) invalid_cast_function_expr

In Dart 1.x dynamic was both a top type (supertype of all types) and a bottom type (subtype of all types) depending on the context. This meant it was valid to assign, for example, a function with a parameter of type String to a place that expected a function type with a parameter of dynamic.

However, in Dart 2 passing something other than dynamic (or another top type, such as Object, or a specific bottom type, such as Null) results in a compile-time error.

Example

  1. typedef Filter = bool Function(dynamic any);
  2. Filter filter = (String x) => x.contains('Hello');
  1. error The function expression type 'bool Function(String)' isn't of type 'bool Function(dynamic)'. This means its parameter or return type doesn't match what is expected. Consider changing parameter type(s) or the returned type(s) invalid_cast_function_expr

Fix: Add type parameters or cast from dynamic explicitly

When possible, avoid this error by adding type parameters:

  1. typedef Filter<T> = bool Function(T any);
  2. Filter<String> filter = (String x) => x.contains('Hello');

Otherwise use casting:

  1. typedef Filter = bool Function(dynamic any);
  2. Filter filter = (x) => (x as String).contains('Hello');

Runtime errors

Unlike Dart 1.x, Dart 2 enforces a sound type system. Roughly, this means you can’t get into a situation where the value stored in a variable is different than the variable’s static type. Like most modern statically typed languages, Dart accomplishes this with a combination of static (compile-time) and dynamic (runtime) checking.

For example, the following type error is detected at compile-time (when the implicit casts option is disabled):

  1. List<int> numbers = [1, 2, 3];
  2. List<String> string = numbers;

Since neither List<int> nor List<String> is a subtype of the other, Dart rules this out statically. You can see other examples of these static analysis errors in Unexpected collection element type.

The errors discussed in the remainder of this section are reported at runtime.

Invalid casts

To ensure type safety, Dart needs to insert runtime checks in some cases. Consider:

  1. assumeStrings(List<Object> objects) {
  2. List<String> strings = objects; // Runtime downcast check
  3. String string = strings[0]; // Expect a String value
  4. }

The assignment to strings is downcasting the List<Object> to List<String> implicitly (as if you wrote as List<String>), so if the value you pass in objects at runtime is a List<String>, then the cast succeeds.

Otherwise, the cast will fail at runtime:

  1. assumeStrings(<int>[1, 2, 3]);
  1. Exception: type 'List<int>' is not a subtype of type 'List<String>'

Fix: Tighten or correct types

Sometimes, lack of a type, especially with empty collections, means that a <dynamic> collection is created, instead of the typed one you intended. Adding an explicit type argument can help:

  1. var list = <String>[];
  2. list.add('a string');
  3. list.add('another');
  4. assumeStrings(list);

You can also more precisely type the local variable, and let inference help:

  1. List<String> list = [];
  2. list.add('a string');
  3. list.add('another');
  4. assumeStrings(list);

In cases where you are working with a collection that you don’t create, such as from JSON or an external data source, you can use the cast() method provided by List and other collection classes.

Here’s an example of the preferred solution: tightening the object’s type.

  1. Map<String, dynamic> json = fetchFromExternalSource();
  2. var names = json['names'] as List;
  3. assumeStrings(names.cast<String>());

Version note: The cast() method was introduced in 2.0.0-dev.22.0.

If you can’t tighten the type or use cast, you can copy the object in a different way. For example:

  1. Map<String, dynamic> json = fetchFromExternalSource();
  2. var names = json['names'] as List;
  3. // Use `as` and `toList` until 2.0.0-dev.22.0, when `cast` is available:
  4. assumeStrings(names.map((name) => name as String).toList());

Appendix

The covariant keyword

Some (rarely used) coding patterns rely on tightening a type by overriding a parameter’s type with a subtype, which is invalid. In this case, you can use the covariant keyword to tell the analyzer that you are doing this intentionally. This removes the static error and instead checks for an invalid argument type at runtime.

Version note: The covariant keyword was introduced in 1.22. It replaces the @checked annotation.

The following shows how you might use covariant:

  1. class Animal {
  2. void chase(Animal x) { ... }
  3. }
  4. class Mouse extends Animal { ... }
  5. class Cat extends Animal {
  6. void chase(covariant Mouse x) { ... }
  7. }

Although this example shows using covariant in the subtype, the covariant keyword can be placed in either the superclass or the subclass method. Usually the superclass method is the best place to put it. The covariant keyword applies to a single parameter and is also supported on setters and fields.