Dart Editor: "unused import" errors

4.2k views Asked by At

For a project, I'm importing a library but I don't use any of the classes in it directly. The goal is to fetch a ClassMirror at runtime to create an instance from it. So I have something like:

import 'controllers.dart';

main() {
    ClassMirror controller = getClassFromString(libraryName: 'deck_app', className: 'HomeController');
    InstanceMirror instance = controller.newInstance(new Symbol(''), []);
    instance.reflectee.sayHey();
}

This gives me an "unused import" error. Idk if this is to be considered a bug. So I'm asking you: do you think this is to be considered as a bug? If not, is there a way I could suppress unused import errors?

What's weird is I thought Dart would tree-shake the source and remove the unused import's code, but it does not. The library is properly imported and available.

1

There are 1 answers

0
Günter Zöchbauer On

The unused import is just a conclusion from the static analyzer. You can ignore it or add a dummy statement to silence the analyzer. This has no effect when you run the application.