Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled exception: type 'A' is not a subtype of type 'A' of 'x' where A is from

Tags:

dart

I get a runtime error that reads something like this:

Unhandled exception:
type 'A' is not a subtype of type 'A' of 'x' where
  A is from file:///path/to/source/a.dart
  A is from package:my_package/a.dart

A is the exact same type as the other A -- there is no naming conflict.

The two indented lines ('A is from ...') only differ in their way of specifying the path. One goes through 'package:' and the second one is the file path on the file system.

like image 980
filiph Avatar asked Mar 10 '23 10:03

filiph


1 Answers

TL;DR:

Use import 'package:...' everywhere, even when importing files from your own package.

Explanation:

The two URLs (file:///... and package:...) are equivalent but Dart has no way of knowing that. When you import source via both relative path and via the package: scheme, you'll get this error.

To fix this issue, make sure to be consistent in how you import files in your own package.

Wrong:

In file foo.dart:

import '../path/to/a.dart';

In file bar.dart:

import 'package:my_package/a.dart';

This results in an error.

Correct:

In file foo.dart:

import 'package:my_package/a.dart';

In file bar.dart:

import 'package:my_package/a.dart';

This will be fine.

like image 83
filiph Avatar answered Mar 12 '23 23:03

filiph