unnecessary_library_name
Don't have a library name in a library
declaration.
This rule is available as of Dart 3.4.
Rule sets: recommended, flutter
This rule has a quick fix available.
Details
#DON'T have a library name in a library
declaration.
Library names are not necessary.
A library does not need a library declaration, but one can be added to attach
library documentation and library metadata to. A declaration of library;
is
sufficient for those uses.
The only use of a library name is for a part
file to refer back to its
owning library, but part files should prefer to use a string URI to refer back
to the library file, not a library name.
If a library name is added to a library declaration, it introduces the risk of name conflicts. It's a compile-time error if two libraries in the same program have the same library name. To avoid that, library names tend to be long, including the package name and path, just to avoid accidental name clashes. That makes such library names hard to read, and not even useful as documentation.
BAD:
/// This library has a long name.
library magnificator.src.helper.bananas;
library utils; // Not as verbose, but risks conflicts.
GOOD:
/// This library is awesome.
library;
part "apart.dart"; // contains: `part of "good_library.dart";`
Usage
#To enable the unnecessary_library_name
rule,
add unnecessary_library_name
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_library_name
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。