Dart 2.8.0 sdk: how to globally ignore omit_local_variable_types warning?

泄露秘密 提交于 2020-07-09 07:24:50

问题


I'm developing a mobile app plus web frontend with Dart / Flutter with IntelliJ Idea. The current version of Dart (2.8.0) warns about correctly typing local variables. There is a Dart style guide https://dart-lang.github.io/linter/lints/omit_local_variable_types.html saying "Usually, the types of local variables can be easily inferred, so it isn't necessary to annotate them."

This might be true for a compiler but it sure is not true for human readers. Since it especially defers type problems to the usage part of a variable, bug detection and code reading is becoming more expensive.

So how can I disable this warning on compiler / project level?

Even better: how can I force a warning if the type is not set?


回答1:


I know this is a bit old, but I see there isn’t an answer, so adding here now for future use.

In the root of your project folder, add an “analysis_options.yaml” file, and include the below code. Read further at: https://dart.dev/guides/language/analysis-options

analysis _options.yaml:

linter:
    rules:
        always_specify_types: true
        omit_local_variable_types: false

Unsure if both are required when always specifying types, but give it a go.




回答2:


Add // ignore: omit_local_variable_types above warning code line:

  // ignore: omit_local_variable_types
  int years = (dif.inDays / 365).floor();


来源:https://stackoverflow.com/questions/59211916/dart-2-8-0-sdk-how-to-globally-ignore-omit-local-variable-types-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!