类似前端使用的 husky,在提交代码前进行代码分析、格式化。

安装 pre-commit

pre-commit

# pip
pip install pre-commit

# brew
brew install pre-commit

项目根目录新建文件:.pre-commit-config.yaml

# See <https://pre-commit.com> for more information
# See <https://pre-commit.com/hooks.html> for more hooks
repos:
  - repo: <https://github.com/dluksza/flutter-analyze-pre-commit>
    rev: "master"
    hooks:
      - id: flutter-analyze

  - repo: <https://github.com/Cretezy/flutter-format-pre-commit>
    rev: "master"
    hooks:
      - id: flutter-format

  - repo: <https://github.com/fluttercommunity/import_sorter>
    rev: "master"
    hooks:
      - id: flutter-import-sorter

安装 git hook scripts

pre-commit install

添加import_sorter dev 依赖

# pubspec.yaml

dev_dependencies:
  import_sorter:

相关钩子及作用