“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

The analysis_optins.yaml file is a static analysis file. In this article we’ll look at the analysis_options.yaml code usage and some of our own ideas.

Here’s a simple example:

include: package:flutter_lints/flutter.yaml
analyzer:
  errors:
   # avoid_print: ignore
     todo: ignore

  avoid_empty_else: ignore
   # exclude: [build/**]
  enable-experiment:
     - extension-methods
  strong-mode:
     implicit-casts: false
     implicit-dynamic: false
     
linter:
   rules:
Copy the code

include

include: package:flutter_lints/flutter.yaml
Copy the code

Importing defined static analysis rule code by importing other files allows you to bring defined external files into your project. The static analysis plugin is introduced in pubspec.yaml case, then the plugin is set in the analysis_options.yaml include.

Flutter_lints: ^ 1.0.0Copy the code

analyzer

analyzer:
  errors:
  exclude:
  enable-experiment:
  strong-mode:
Copy the code

Set custom content for the entire static analysis.

errors

errors:
  avoid_print: ignore
Copy the code

The rule name on the left and four types on the right: Ignore, INFO, Warning, and error

When introducing the above static analysis plug-in, if you want to disable certain rules. Analyzer The errors option is set to ignore to disable rules specified in the entire project. The rest is the literal meaning of messages, warnings, errors. You can also use ignore_for_file instead of ignore.

// ignore: avoid_print print('Lineter Test'); // You can use ignore_for_file: to remove warnings for certain rules in the entire project, separate rules with a comma. // ignore_for_file: prefer_const_constructors Text('save network image to photo')Copy the code

exclude

exclude:
  - lib/client.dart
  - lib/data/model/*.freezed.dart
  - test/_data/**
Copy the code

You can set rules not to apply to certain files or exclude certain files from static analysis plug-in analysis.

enable-experiment

enable-experiment:
   - extension-methods
Copy the code

Open the expansion suite method, add the Settings above, and ensure that the Dart SDK >= 2.6.0

strong-mode

Enable strict type checking. Implicit type conversions or relatively loose type checking are enabled by default. If you need to set up type checking that is more stringent than Dart’s default type checking, you can do the following with Analyzer in strong-Mode:

strong-mode:
   implicit-casts: false
   implicit-dynamic: false
Copy the code

implicit-casts

Implicit-casts: false Implicit casts can be set to disable implementations of implicit type casts. In order not to write more than a few characters, this leads to potentially unsafe code. This implicit-cast function is related to variable assignment. Implicit-casts are turned on in order not to write several characters, resulting in potentially unsafe code. Same thing with implicit-dynamic.

Not disabled:

num a = 42; // we assigned an `int` to `num`, this is valid``
// UNSAFE! We assigned `num` to `double`. This is valid with `implicit-cast` only.
// This will crash at runtime, as `baseClass` contains an `int` here.
double b = a;
Copy the code

If disabled, the above code will not compile successfully. But we can fix this compilation error by casting:

num a = 42; double b = a as double; Or double c = a is double? A: 0.0.Copy the code

implicit-dynamic

Implicit-dynamic: false Implicit dynamic dynamic disallows the use of types in dynamic type declarations. Implicit-dynamic is about type inference. If enabled, implicit-dynamic will cause the type to fall back to Dynamic when type inference fails.

By disabling 'implicit-dynamic', this will make our declaration no longer compile:Copy the code
 var list = []; // Compile error: Missing type argument for list literal.
Copy the code

You can specify the type to fix the code, as follows:

var list = <int>[];
Copy the code

If you add a specified type or use a specified type, the following error will be reported:

    list.add(42); // works
    list.add('hello world'); // The argument type 'String' can't be assigned to the parameter type 'int'.dartargument_type_not_assignable
    for (final item in list) {
       // Compile error, no property `somePropertyThatDoesNotExist` on `int`
      print(item.somePropertyThatDoesNotExist);
    }
Copy the code

Finally, it is recommended that you enable strict type checking whenever possible, because it makes type checking more rigorous at compile time and makes your code more rigorous.

linter

linter:
   rules:
Copy the code

Here are some lint rules to enable. The full list of Lint rules can be found here. But because Lint has so many rules, there are more than 160 rules, some of which are added regularly. Knowing what is enabled and what is not in a single file can be difficult and cumbersome. So how do you manage lint rules?

To solve this problem, I recommend creating two different files:

  • aall_lint_rules.yamlFile to enableallThe rules. This is aAll rule listsLocal copy.
  • analysis_options.yamlFile to importall_lint_rules.yamlFile and disable rules you don’t want.
 linter:
    rules:
      avoid_print: false
Copy the code

This has two advantages:

  • First know what is forbidden, because weanalysis_options.yamlNo longer contains enabled rules, only disabled rules.
  • Second, it’s easy to maintain our list when adding new rules. Just copy and paste the official list into oursall_lint_rules.yamlIn the file.

That concludes the analysis_options.yaml file. The next article will talk about the first code in Main.dart:

void main() {
   runApp(const MyApp());
}
Copy the code