Difference of HTML and Dart imports

Issue

Using Polymer Dart 1.0.0-rc.6, is there a difference or any recommendation about html imports using <link rel ...> vs Dart’s import syntax; import '...'?

If I use Dart’s import, usually it will leave out a warning since I only import for the “side effect” of having the element loaded in Polymer, but not actually using it in Dart code, in which case I can suppress warnings by simply typing some class name that exists in the imported Dart Polymer element somewhere, followed by ;, however some dart polymer elements don’t have any classes exposed, so I can’t suppress the warnings in such cases, for example: ‘package:polymer_elements/iron_icons.dart’; has no exported classes which I can simply type out to suppress.

In other words, does it matter if I import the elements using dart’s import mechanism, or via HTML import? are there any plans by the dart team to support suppressing such warnings?

Solution

Dart import import '...'; is the way to go. I haven’t seen <link rel="import" > recommended anywhere.

Another workaround is to reference an exported identifier in a Dart doc comment

import 'package:polymer_elements/paper_item.dart';

/// Silence analyzer [PaperItem]
@PolymerRegister('some-element')
class SomeElement extends PolymerElement { ...

If I have style modules (HTML-only) where the *.dart file’s only purpose is to be able to import the style module using Dart imports, I add some identifier like

const myStyleSilence = 0;

to be able to reference it in Dart doc comments to silence the unused import warning.

The main advantage of Dart imports is that the weird and something complicated rules to build the correct path for <link rel="import" src="..."> can be avoided (see https://www.dartlang.org/polymer-old/app-directories.html#into-a-non-dart-file-1).
Refactoring support works (or should work) better with Dart imports.

I certainly hope that it will be possible to suppress unused import warnings eventually. A lot of work is going on to make the analyzer more configurable.
There is an issue from a member of the Polymer.dart team https://github.com/dart-lang/sdk/issues/22660

Answered By – Günter Zöchbauer

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.