搜索插件、代码仓与 APP 分类

插件详情v4.6.0

import_sorter

自动整理您的 dart 导入

AndroidiOSmacOSWindowsLinux
近 30 天下载
9.3万
喜欢数
347
Pub 分数
130

安装插件

flutter pub add import_sorter

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Import Sorter 是一个用于 Flutter 的开发者工具,可自动整理 Dart 导入语句。它确保导入语句排序一致,提高代码的可读性和可维护性。该包可无缝集成到开发工作流中,支持所有主要平台,包括 Android、iOS、macOS、Windows 和 Linux。通过运行一个简单命令,开发者即可根据预设规则格式化导入列表,减少手动操作并降低合并冲突的风险。非常适合需要在项目间统一代码风格的团队。

适合场景

  • 自动化导入组织
  • 强制执行一致的代码风格
  • 减少合并冲突
  • 提高代码可读性
  • 优化团队工作流程

主要特点

  • 自动导入排序
  • 命令行集成
  • 支持多个平台
  • 防止手动导入错误
  • 与 Flutter 工作流集成

适合对象

  • Flutter 团队
  • 代码质量倡导者
  • 大型项目
  • CI/CD 流水线
  • 开发人员优先考虑一致性

使用建议

  • 需要在项目中进行设置
  • 可能与其他格式化程序冲突
  • 需要定期更新
  • 依赖于正确配置
  • 不适合一次性脚本

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

 ___  _____ ______   ________  ________  ________  _________
|\  \|\   _ \  _   \|\   __  \|\   __  \|\   __  \|\___   ___\
\ \  \ \  \\\__\ \  \ \  \|\  \ \  \|\  \ \  \|\  \|___ \  \_|
 \ \  \ \  \\|__| \  \ \   ____\ \  \\\  \ \   _  _\   \ \  \
  \ \  \ \  \    \ \  \ \  \___|\ \  \\\  \ \  \\  \|   \ \  \
   \ \__\ \__\    \ \__\ \__\    \ \_______\ \__\\ _\    \ \__\
    \|__|\|__|     \|__|\|__|     \|_______|\|__|\|__|    \|__|
    ________  ________  ________  _________  _______   ________
   |\   ____\|\   __  \|\   __  \|\___   ___\\  ___ \ |\   __  \
   \ \  \___|\ \  \|\  \ \  \|\  \|___ \  \_\ \   __/|\ \  \|\  \
    \ \_____  \ \  \\\  \ \   _  _\   \ \  \ \ \  \_|/_\ \   _  _\
     \|____|\  \ \  \\\  \ \  \\  \|   \ \  \ \ \  \_|\ \ \  \\  \|
       ____\_\  \ \_______\ \__\\ _\    \ \__\ \ \_______\ \__\\ _\
      |\_________\|_______|\|__|\|__|    \|__|  \|_______|\|__|\|__|
      \|_________|

Request for a Maintainer

This project needs a maintainer as I no longer work with dart and flutter. Please see the issue thread for more information.

import_sorter Pub Version

Flutter Community: import_sorter

test format

🎯 Dart package to automatically organize your dart imports. Any dart project supported! Will sorts imports alphabetically and then group them in the following order:

  1. Dart imports
  2. Flutter imports
  3. Package imports
  4. Project imports

Below is an example:

Before
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/physics.dart';
import 'package:flutter/painting.dart';
import 'package:intl/intl.dart';
import 'package:mdi/mdi.dart';
import 'package:provider/provider.dart';
import 'anotherFile.dart';
import 'package:example_app/anotherFile2.dart';
import 'dart:async';
import 'dart:io';
import 'dart:js';
After
// Dart imports:
import 'dart:async';
import 'dart:io';
import 'dart:js';

// Flutter imports:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/physics.dart';

// Package imports:
import 'package:intl/intl.dart';
import 'package:mdi/mdi.dart';
import 'package:provider/provider.dart';

// Project imports:
import 'package:example_app/anotherFile2.dart';
import 'anotherFile.dart';

🚀 Installing

Simply add import_sorter: ^4.6.0 to your pubspec.yaml's dev_dependencies.

🏃‍♂️ Running

Once you've installed it simply run flutter pub run import_sorter (pub run import_sorter if normal dart application) to format every file dart file in your lib, bin, test, and tests folder! Don't worry if these folders don't exist.

💻 Command Line

  • Add the -e flag to the run command and have emojis added to your imports 😄.
  • If you're using a config in the pubspec.yaml you can have the program ignore it by adding --ignore-config.
  • Want to make sure your files are sorted? Add --exit-if-changed to make sure the files are sorted. Good for things like CI.
  • Have no comments before your imports by adding the --no-comments flag.
  • Add the -h flag if you need any help from the command line!
  • You can only run import_sorter on certain files by passing in a regular expression(s) that will only sort certain files. Below are two examples:
    • pub run import_sorter bin/main.dart lib/args.dart (only sorts bin/main.dart and lib/args.dart)
    • pub run import_sorter lib\/* test\/* (only sorts files in the lib and test folders)

🏗️ Config

If you use import_sorter a lot or need to ignore certain files you should look at using the config you put in your pubspec.yaml. Ignored files are in the format of regex. This regex is then applied to the project root path (the one outputted to the terminal). Below is an example config setting emojis to true and ignoring all files in the lib folder:

import_sorter:
  emojis: true # Optional, defaults to false
  comments: false # Optional, defaults to true
  ignored_files: # Optional, defaults to []
    - \/lib\/*

If you need another example check the example app's import_sorter configuration.

🚨 pre-commit Hook

There are two pre-commit hooks available: dart-import-sorter and flutter-import-sorter. They use pub run and flutter pub run respectively. Use the former for a generic Dart project and the latter for a Flutter project.

Using pre-commit hooks in your project:

  • Install and configure pre-commit.
  • Install and configure import_sorter using instructions above.
  • Add the following to the repos section of your .pre-commit-config.yaml:
- repo: https://github.com/fluttercommunity/import_sorter
  rev: 'master'
  hooks:
    - id: dart-import-sorter # use `flutter-import-sorter` for a Flutter project
  • Run initial sort:
pre-commit run --all-files

🙋‍♀️🙋‍♂️ Contributing

All contributions are welcome! Just make sure that it's not an already existing issue or pull request.

👥 Contributors

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目