पाठ 20 / 25

Static Analysis, Lints and Formatting

Configure analysis_options.yaml, apply lints and automate fixes.

Quality checks built in

Dart's analyser runs continuously in your editor and with dart analyze, reporting type errors, null-safety problems and lints. Configure it in analysis_options.yaml: include a recommended rule set (package:lints/recommended.yaml for Dart, package:flutter_lints/flutter.yaml for Flutter), enable stricter language modes (strict-casts, strict-inference, strict-raw-types), and enable or disable individual lint rules such as unawaited_futures, prefer_const_constructors, avoid_print and require_trailing_commas. dart fix --apply applies automated fixes suggested by the analyser, including migrations for deprecated APIs. dart format enforces the official style with no configuration debates (apart from page width), and recent versions use a "tall" style with trailing commas managed automatically. Treat warnings as errors in CI with dart analyze --fatal-infos or --fatal-warnings. Code generation tools (build_runner with json_serializable or freezed) generate boilerplate such as JSON methods and copyWith; keep generated files out of manual edits and regenerate them in the build.

A strict analysis_options.yaml

Recommended lints plus stricter type checks and a few extra rules.

include: package:lints/recommended.yaml

analyzer:
  language:
    strict-casts: true
    strict-inference: true
    strict-raw-types: true
  exclude:
    - "**/*.g.dart"
    - "**/*.freezed.dart"

linter:
  rules:
    - unawaited_futures
    - prefer_final_locals
    - prefer_const_constructors
    - avoid_dynamic_calls
    - always_declare_return_types

# CI:
#   dart format --output=none --set-exit-if-changed .
#   dart analyze --fatal-infos
#   dart test

A strict but helpful editor

The analyser is an editor who reads every sentence as you type and circles mistakes before anyone else sees them. Stricter settings mean more red ink at first, but far fewer embarrassing errors in print.

त्वरित जाँच: Which command applies automated fixes suggested by the Dart analyser?

  • dart format
  • dart pub upgrade
  • dart compile
  • dart fix --apply
Answer

dart fix --apply — dart fix applies analyser-provided fixes, including API migrations.