___ _____ ______ ________ ________ ________ _________
|\ \|\ _ \ _ \|\ __ \|\ __ \|\ __ \|\___ ___\
\ \ \ \ \\\__\ \ \ \ \|\ \ \ \|\ \ \ \|\ \|___ \ \_|
\ \ \ \ \\|__| \ \ \ ____\ \ \\\ \ \ _ _\ \ \ \
\ \ \ \ \ \ \ \ \ \___|\ \ \\\ \ \ \\ \| \ \ \
\ \__\ \__\ \ \__\ \__\ \ \_______\ \__\\ _\ \ \__\
\|__|\|__| \|__|\|__| \|_______|\|__|\|__| \|__|
________ ________ ________ _________ _______ ________
|\ ____\|\ __ \|\ __ \|\___ ___\\ ___ \ |\ __ \
\ \ \___|\ \ \|\ \ \ \|\ \|___ \ \_\ \ __/|\ \ \|\ \
\ \_____ \ \ \\\ \ \ _ _\ \ \ \ \ \ \_|/_\ \ _ _\
\|____|\ \ \ \\\ \ \ \\ \| \ \ \ \ \ \_|\ \ \ \\ \|
____\_\ \ \_______\ \__\\ _\ \ \__\ \ \_______\ \__\\ _\
|\_________\|_______|\|__|\|__| \|__| \|_______|\|__|\|__|
\|_________|
This project needs a maintainer as I no longer work with dart and flutter. Please see the issue thread for more information.
🎯 Dart package to automatically organize your dart imports. Any dart project supported! Will sorts imports alphabetically and then group them in the following order:
Below is an example:
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';
// 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';
Simply add import_sorter: ^4.6.0
to your pubspec.yaml
's dev_dependencies
.
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.
-e
flag to the run command and have emojis added to your imports 😄.pubspec.yaml
you can have the program ignore it by adding --ignore-config
.--exit-if-changed
to make sure the files are sorted. Good for things like CI.--no-comments
flag.-h
flag if you need any help from the command line!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)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
HookThere 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:
pre-commit
.import_sorter
using instructions above.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
pre-commit run --all-files
All contributions are welcome! Just make sure that it's not an already existing issue or pull request.