如何仅用于特定导入而不是整个文件来抑制dewed_on_referenced_packages?
升级到Flutter 3.0后,我开始获得大量depend_on_referended_packages
在运行flutter Analyaze
时。如果我删除报告的导入汇编断裂。大多数问题都是package:collection/collection.dart
提供扩展方法。
目前,我能找到的最好的是通过添加// ighore_for_file:dived_on_referended_packages
来抑制受影响文件的此皮棉。但是,我喜欢这个皮棉(总的来说,我想尽可能多地启用绒毛),因此,是否有一种方法可以仅仅抑制棉绒,而不是仅仅用于特定的导入而不是文件中的所有导入?
pubspec.yaml
的相关部分:
...
environment:
sdk: ">=2.17.1 <3.0.0"
flutter: ">=3.0.1"
...
dev_dependencies:
flutter_lints: ^2.0.1
运行:
$ flutter analyze
...
info • Depend on referenced packages • lib/preferences/palette_spec.dart:3:8 • depend_on_referenced_packages
info • Depend on referenced packages • lib/ui/parts/circular_menu.dart:5:8 • depend_on_referenced_packages
...
源代码示例:
database_utils.darts.dart
(FirstWhereOrnull来自Collection.dart.dart):
...
import 'package:collection/collection.dart';
...
Activity? _getActivityById(int id) {
return activities.firstWhereOrNull((element) => element.id == id);
}
...
Record? _getRecordById(int id) {
return records.firstWhereOrNull((element) => element.id == id);
}
PALETTES_SPEC.DART
( foreachIndexed来自collection.dart):
...
import 'package:collection/collection.dart';
...
paletteStr.split(",").forEachIndexed((index, colorStr) {
...
});
coundular_menu.dart
:
...
import 'package:vector_math/vector_math.dart' as vector;
...
final angle = vector.radians(90.0 / (widget.children.length - 1) * index + angleFix);
注意:根本原因是该集合被带入了一种及其依赖性。
最初,我误解了皮棉。对PETR的解决方案的解释:当他说“报道您依赖于及时依赖性”时,这意味着我的代码中的某个地方我有一个从该依赖项中导入内容的导入。但是,在皮棉时,依赖性仅是传递性的,而不是直接的。因此,如果我出于某种原因决定不依赖包装,这突然间我会出现错误的蓝色。皮棉试图使该依赖图更直接。
After upgrading to Flutter 3.0 I started to get a lot of depend_on_referenced_packages
lint issues when running flutter analyze
. If I remove the reported imports the compilation breaks. Most of the issues are with package:collection/collection.dart
which provides extension methods.
Currently the best I could find is to suppress this lint for the affected files by adding // ignore_for_file: depend_on_referenced_packages
. However I like this lint (and in general I'd want to keep as much lints enabled as possible), so is there a way to suppress the lint only for a just a specific import and not for all imports in the file?
Relevant sections of pubspec.yaml
:
...
environment:
sdk: ">=2.17.1 <3.0.0"
flutter: ">=3.0.1"
...
dev_dependencies:
flutter_lints: ^2.0.1
Running:
$ flutter analyze
...
info • Depend on referenced packages • lib/preferences/palette_spec.dart:3:8 • depend_on_referenced_packages
info • Depend on referenced packages • lib/ui/parts/circular_menu.dart:5:8 • depend_on_referenced_packages
...
Source code examples:
database_utils.dart
(firstWhereOrNull is from collection.dart):
...
import 'package:collection/collection.dart';
...
Activity? _getActivityById(int id) {
return activities.firstWhereOrNull((element) => element.id == id);
}
...
Record? _getRecordById(int id) {
return records.firstWhereOrNull((element) => element.id == id);
}
palette_spec.dart
(forEachIndexed is from collection.dart):
...
import 'package:collection/collection.dart';
...
paletteStr.split(",").forEachIndexed((index, colorStr) {
...
});
circular_menu.dart
:
...
import 'package:vector_math/vector_math.dart' as vector;
...
final angle = vector.radians(90.0 / (widget.children.length - 1) * index + angleFix);
Note: the root cause is that collection is brought in as a transitive dependency.
Originally I misunderstood the lint. Explanation to Petr's solution: when he says "lint is reported if you depend on a transitive dependency" it means that somewhere in my code I have an import which imports stuff from that dependency. But at the time of the lint that dependency is only transitive, not direct. Therefore if I'd decide - for whatever reason - to not depend on the package which brings that in then suddenly I'd have an error out o the blue for that import. The lint tries to make that dependency graph more direct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您取决于及时的依赖性,则报告此皮棉。您在PubSpec.yaml定义的收藏中是否有?
我遇到了同样的问题,但是在定义依赖性之后,皮棉问题已经消失。
This lint is reported if you depend on a transitive dependency. Do you have in your pubspec.yaml defined collection?
I had same issue but after defining dependency, lint issue is gone.