Dart : How to ignore omit_local_variable_types warning?
Asked Answered
C

3

8

I'm developing a mobile app plus web frontend with Dart / Flutter with IntelliJ Idea. The current version of Dart 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?

Chromatism answered 6/12, 2019 at 11:5 Comment(2)
Where did you get that version? Latest stable version of Dart is currently 2.6.1 and the dev version are 2.7.0-dev.2.1: dart.dev/tools/sdk/archiveQuickie
Told by Idea -> Settings -> Languages -> Dart it says: "Version: 2.8.0-edge.413..."Chromatism
K
16

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.

Keeney answered 11/1, 2020 at 3:38 Comment(0)
E
8

To ignore the warnings only for a specific file:

// ignore_for_file: omit_local_variable_types

class Foo {
  // ...
}
Erotomania answered 6/11, 2020 at 17:47 Comment(0)
C
4

Add // ignore: omit_local_variable_types above warning code line:

  // ignore: omit_local_variable_types
  int years = (dif.inDays / 365).floor();
Colpitis answered 23/1, 2020 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.