🎯 Automatically organize your dart imports. Maintainer: @gleich

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

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:main (pub run import_sorter:main 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:main bin/main.dart lib/args.dart (only sorts bin/main.dart and lib/args.dart)
    • pub run import_sorter:main 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

Comments
  • flutter_gen Package Import Gets Deleted

    flutter_gen Package Import Gets Deleted

    Describe the bug Without a comment on the same line the import 'package:flutter_gen/gen_l10n/translations.dart'; gets removed when running flutter pub run import_sorter:main.

    To Reproduce You can just add the import to your main.dart file after creating a new project. It won't be able to build, but running flutter pub run import_sorter:main after saving the file still deletes the import. Note: even after there are no build errors it still deletes it

    Expected behavior The import should not be deleted.

    Meta Information:

    • Dart Version: 2.10.2
    • import_sorter Version: 4.2.2
    • Is it a Flutter project? Yes
    • What version of Flutter are you using (if flutter project): 1.22.2

    Additional Context The import is sorted correctly if you add a comment to the same line. For example: import 'package:flutter_gen/gen_l10n/translations.dart'; // gets removed

    import 'package:flutter_gen/gen_l10n/translations.dart'; // import_sorter:keep // gets sorted correctly

    bug 
    opened by supposedlysam-bb 24
  • Add argument to sort some files only?

    Add argument to sort some files only?

    Hi, I would like to use this together with my git hook, and have my changed files sorted only. It would be great to having a way to sort only a subset of files, instead of sorting all of them.

    opened by crizant 13
  • Precommit hook doesn't sort imports

    Precommit hook doesn't sort imports

    Describe the bug Precommit hook doesn't sort imports

    To Reproduce What command or actions did you take to produce this error? Followed the steps at https://pub.dev/packages/import_sorter When I run 'pre-commit run --all-files' nothing happens

    .pre-commit-config.yaml:

    repos:
        - repo: https://github.com/pre-commit/pre-commit-hooks
          rev: v3.4.0
          hooks:
              - id: trailing-whitespace
              - id: end-of-file-fixer
              - id: check-yaml
              - id: check-added-large-files
        - repo: https://github.com/fluttercommunity/import_sorter
          rev: "master"
          hooks:
              - id: flutter-import-sorter
    
    

    I got it working with this hotfix as .pre-commit-config.yaml:

    repos:
        - repo: https://github.com/pre-commit/pre-commit-hooks
          rev: v3.4.0
          hooks:
              - id: trailing-whitespace
              - id: end-of-file-fixer
              - id: check-yaml
              - id: check-added-large-files
        - repo: local
          hooks:
              - id: imputsorter
                name: imputsorter
                entry: flutter pub run import_sorter:main
                language: system
                files: ^lib/
    

    It may have something to do with the 'files: .dart$'

    Meta Information:

    • Flutter Version: 1.24.0-10.2
    • import_sorter Version: ^4.4.1
    bug 
    opened by martinloretzzz 12
  • Archiving `import_sorter`

    Archiving `import_sorter`

    Hello everyone. I'm sad to say that today I am going to be archiving import_sorter. Unfortunately I no longer work with dart and flutter anymore and as I go into college I don't have a lot of free time to maintain the project. Anyone is free to fork the code or take over as maintainer but I am going to be archiving the project today. Sorry for any inconveniences this may cause and thank you so much for all the support.

    opened by gleich 10
  • RangeError after executing flutter pub run import_sorter:main

    RangeError after executing flutter pub run import_sorter:main

    Describe the bug After executing the flutter pub run import_sorter:main command I get following error:

    username@PC044-MacOS flutter_my_app % flutter pub run import_sorter:main
    
    β”β”β”πŸ—‚  Sorting 94 dart files
    ┃  ┃
    ┃  ┣━━ βœ… Sorted 0 out of 7 imports in lib/drawer/drawer_navigation.dart
    ┃  ┣━━ βœ… Sorted 0 out of 1 imports in lib/drawer/naviagtion_router.dart
    Unhandled exception:
    RangeError (index): Invalid value: Not in inclusive range 0..1: 2
    #0      List.[] (dart:core-patch/growable_array.dart:177:60)
    #1      main (file:///Users/username/development/flutter/.pub-cache/hosted/pub.dartlang.org/import_sorter-4.4.0/bin/main.dart:97:149)
    #2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
    #3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
    pub finished with exit code 255
    

    To Reproduce I installed the import_sorter package and did run flutter pub run import_sorter:main

    Expected behavior correct sorting, but it stopped after two files.

    Screenshots Bildschirmfoto 2020-11-23 um 17 25 56

    Meta Information:

    • Dart Version: 2.10.4
    • import_sorter Version: 4.4.0
    • Is it a Flutter project? Yes
    • What version of Flutter are you using (if flutter project): 1.22.4
    bug 
    opened by KevinRohn 10
  • Don't delete newlines in one-line files

    Don't delete newlines in one-line files

    Describe the bug In my CI, I use (or at least, I would love to use, because this very issue prevents me from doing so) dart format -l 100 --output none --set-exit-if-changed . to format my code. After that, I want to check if my imports are sorted using flutter pub run import_sorter:main --exit-if-changed. Now, it is just not possible for those two actions to complete successfully (they're mutually exclusive), and that is because import_sorter removes trailing newlines from files without any import ... statements (or at least it looks like so).

    File 1 Example file that causes problems (i.e trailing newline is removed by import_sorter):

    enum HomeEvent { showInfo, showDiscover, showProfile }
    

    File 2 Example file that works fine (i.e trailing newline is not removed by import_sorter):

    import 'dart:core';
    
    enum HomeEvent { showInfo, showDiscover, showProfile }
    

    To Reproduce

    1. Create a file with contents of file 1
    2. dart format -l 100 .
    3. The file will have 2 lines: 1 line of code and a trailing newline. That's what I want.
    4. flutter pub run import_sorter:main --no-comments
    5. Check file contents. There won't be a trailing newline at the end of the file. I don't want this.
    6. dart format -l 100 .. A trailing newline will be there again. Yay!

    Expected behavior import_sorter should not delete the trailing newline.

    Useful resources

    • How to easily check if a file has a trailing newline
    • according to my experience (I'm on macOS), the easiest way to edit a file is using TextEdit.app (it does not insert a trailing newline automatically, while several other programs I've used do insert a trailing newline (e.g nano)). To open a file in with TextEdit, use open -e <file>

    Meta Information:

    • Dart Version: 2.12.3
    • import_sorter Version: 4.5.0
    • Is it a Flutter project? Yes
    • What version of Flutter are you using (if flutter project): 2.0.6
    bug 
    opened by bartekpacia 6
  • Distinguish what files were changed in the output

    Distinguish what files were changed in the output

    Is your feature request related to a problem? Please describe. It is not clear what files were changed from the import_sorter output. In fact. all files appear to be changed. I would like to review files that were changed by the tool. This could be especially useful when running import_sorter as a pre-commit hook.

    Describe the solution you'd like I would like to see just an "Ok" for files that were not changed and the current output for those that were.

    Describe alternatives you've considered There is none.

    enhancement 
    opened by lig 6
  • Keep code before imports

    Keep code before imports

    I just ran the importer and it broke the project.

    Reviewing the file that was reported by the analyzer as broken and comparing to GitHub I saw that library XXXXXX; had been stripped.

    bug 
    opened by nodinosaur 5
  • Sort imports without adding comments

    Sort imports without adding comments

    Thanks for this package! I've got a little suggestion.

    I like the idea to sort my imports, but I would like to have the comments removed. A flag to sort the imports without adding comments like "Flutter imports:" would be great.

    enhancement 
    opened by DFelten 4
  • Can generated files be ignored?

    Can generated files be ignored?

    Is your feature request related to a problem? Please describe. When I run code generation, the sorting and comments for imports are overridden and they aren't very relevant for generated code anyway.

    Describe the solution you'd like The option to provide a regular expression like **/*.g.dart that skips files matching it.

    enhancement 
    opened by britannio 4
  • "Failed to run flutter pub get"

    Describe the bug When flutter pub run import_sorter:main is run, it exits with exit code 127, and fails to run flutter pub get. However, when flutter pub get is run individually of flutter pub run import_sorter:main, it works fine, and running flutter pub get -v produces the expected output, all exit codes 0.

    To Reproduce I ran flutter pub run import_sorter:main. I am using import_sorter 3.0.5, Ubuntu 19.10, Flutter 1.8.0-11.1.pre on channel beta, and Dart 2.9.0-8.2.beta.

    Expected behavior I expected the program to run properly, or at the very least, not fail on flutter pub get, but rather another step.

    Screenshots image image

    Meta Information:

    • Dart Version:
    • import_sorter Version: ^3.0.5
    • Is it a Flutter project? Yes.
    • What version of Flutter are you using (if flutter project): 1.8.0-11.1.pre on channel beta
    bug 
    opened by Cal-Hagner 4
  • Specify config in an independent file

    Specify config in an independent file

    Is your feature request related to a problem? Please describe. We are migrating our projects to a single monorepo and would love to have only one configuration set for all the stuff related to import sorting. Now, as ignoring files are only supported to be specified in the pubspec.yaml file we have to replicate that config per project instead of just once at the root of the monorepo.

    Describe the solution you'd like We would love to specify the config of import_sorter in a YAML file which could be passed using a relative path to the sort command.

    Describe alternatives you've considered I've tried to build a negative REGEX and pass it to the command (e.g. sort all imports here except those ending in *.g.dart), but without success. If such a regex is possible, no config file would be needed as all the config options could be passed in one global command.

    opened by Pitazzo 0
  • Possibility to add extra line above imports

    Possibility to add extra line above imports

    You can add extra key in pubspec:

    import_sorter:
      header: 'Extra header above' # Optional, adds extra line above all imports.
    

    Result:

    // Extra header above!
    
    // 🎯 Dart imports:
    import 'dart:io';
    
    .....
    
    opened by scorpionate 0
  • Convert project imports to relative imports

    Convert project imports to relative imports

    Closes #59

    • Add conversion of project imports to relative imports for lib files.
    • Add test for convertToRelativeImports

    Question: The example app has the following import but no corresponding dependency import 'package:flutter_gen/gen_l10n/translations.dart';

    opened by gupta-shrinath 3
  • Guidelines / solution to run compiled version

    Guidelines / solution to run compiled version

    Is your feature request related to a problem? Please describe. The current execution of flutter pub run import_sorter:main has a slow start due to the Dart VM initialisation. While it is acceptable if it is ran infrequently (like as a GIT commit hook for example), it is a lot more critical when running frequently (like every time a file is saved for example).

    Describe the solution you'd like

    • Give users guidelines to use the natively compiled version of the library rather that its Dart version
    • OR find a way to distribute the compiled versions

    Describe alternatives you've considered I have tested the execution time of this library for three different versions:

    • 1: The Dart version, as described in the README
    • 2: The AOT compiled version, as described in https://dart.dev/tools/dart-compile#aot-snapshot
    • 3: The executable compiled version, as described in https://dart.dev/tools/dart-compile#exe

    Results:

    • 1 was the slowest, with an initialisation time of about 1 to 2 seconds on a i9 2019 Macbook Pro 16'.
    • 2 was faster, maybe twice faster.
    • 3 was the fastest, almost instantaneous

    Additional context The benefits of running a compiled version are also explained in https://dart.dev/tutorials/server/get-started#6-compile-for-production.

    This is what I've done to get a nice experience using this lib:

    1. Compile to native code
    dart compile exe bin/main.dart -o bin/import_sorter
    
    1. Add bin path to PATH
    2. Use lib in VSCode with https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave I've set it up with the following settings:
      "emeraldwalk.runonsave": {
        "commands": [
          {
            "match": "\\.dart$",
            "cmd": "import_sorter ${file}"
          }
        ]
      }
    
    

    Now every time I save a file in my IDE, the imports are organised instantaneously! This makes it a lot more enjoyable.

    opened by vkammerer 1
  • Feature Request: Add option for relative import

    Feature Request: Add option for relative import

    It would be great if this package can change our absolute imports to relative imports.

    Example 1 import 'package:my_app/lib/src/foo.dart' to import 'foo.dart'

    Example 2

    import 'package:my_app/lib/src/p1/foo.dart'
    import 'package:my_app/lib/src/p2/foo.dart'
    

    to

    import 'p1/foo.dart'
    import 'p2/foo.dart'
    

    if imported in src to

    import '../p1/foo.dart'
    import 'foo.dart'
    

    if imported in p2

    I would like to contribute to this feature but can anyone guide me with this?

    opened by SirusCodes 1
Owner
Flutter Community
A central place for all community made Flutter packages. To get started, see the README of the 'community' repository.
Flutter Community
Gallery with thousands of photos, no time to organize, no way to search and not enough space

Artificial Intelligence-Powered Gallery Overview Gallery with thousands of photos, no time to organize, no way to search and not enough space; these a

Abd al-Rahman al-Ktefane 4 Jul 25, 2022
Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur

❀️ Sponsor get_it This is a simple Service Locator for Dart and Flutter projects with some additional goodies highly inspired by Splat. It can be used

Flutter Community 1k Jan 1, 2023
Responsive Scaffold - On mobile it shows a list and pushes to details and on tablet it shows the List and the selected item. Maintainer: @rodydavis

responsive_scaffold View the online demo here! On mobile it shows a list and pushes to details and on tablet it shows the List and the selected item.

Flutter Community 346 Dec 2, 2022
State Persistence - Persist state across app launches. By default this library store state as a local JSON file called `data.json` in the applications data directory. Maintainer: @slightfoot

State Persistence Persist state across app launches. By default this library store state as a local JSON file called data.json in the applications dat

Flutter Community 70 Sep 28, 2022
Cross-platform flutter plugin for reading and writing NFC tags. Not maintained anymore - not looking for new maintainer, fork instead.

nfc_in_flutter NFC in Flutter is a plugin for reading and writing NFC tags in Flutter. It works on both Android and iOS with a simple stream interface

Andi Semler 113 Sep 28, 2022
Automatically generate profile picture with random first name and background color. But you can still provide pictures if you have them. As the default color, based on the name of the first letter. :fire: :fire: :fire:

FLUTTER PROFILE PICTURE Automatically generate profile picture with random first name and background color. But you can still provide pictures if you

Aditya Dharmawan Saputra 10 Dec 20, 2022
Purpose of this project is to create extendable architecture of making platform aware Widgets which automatically select platform specific implementation

Old good factory Main obstacle in creating native experience on Flutter is the fact that you are asked to rebuild two layouts using platform specific

Swav Kulinski (Robotoaster) 101 Oct 14, 2022
An extension to the bloc state management library which automatically persists and restores bloc states.

⚠️ Attention: This repository has been moved to https://github.com/felangel/bloc and is now read-only! An extension to the bloc state management libra

Felix Angelov 189 Nov 17, 2022
An android app that can automatically dial a phone number.

AutoCallScheduler An android app that can automatically dial a phone number within a given scheduled of time. Basically it's a test base app of google

Rezwan 24 Dec 4, 2022
A tool which automatically generates Flutter localization resources from CSV and Excel files.

flappy_translator A tool which automatically generates Flutter localization resources from CSV and Excel files. This is especially useful as any team

Smart&Soft 55 Sep 15, 2022
Spartial - A flutter app that automatically skips the lesser parts of Spotify songs

Spartial (Spartial.app) Mobile app that lets you automatically skip parts of Spo

Ruud Brouwers 3 Mar 16, 2022
This widget automatically scrolls the custom child widget to an infinite loop.

Scroll Loop Auto Scroll This widget automatically scrolls the custom child widget to an infinite loop. Example Features Infinite Auto Scroll Custom ch

Ashish Raturi 9 Dec 12, 2022
Scale the entire UI design proportionally. Button, image, font, everything is scaled automatically

Scale the entire UI design proportionally. Button, image, font, everything is scaled automatically. Before: 250x250 square is the same size across dev

Wildcard 3 Sep 25, 2022
Automatically create a new Flutter project with Getx & Directories

Flucreator You can use Flucreator to create a new Flutter project. Automatically create a new Flutter project with Getx & Directories. ?? Normal Usa

Abdussamet Inanç 6 Jul 8, 2022
🎯 This library automatically generates object classes from JSON files that can be parsed by the freezed library.

The Most Powerful Way to Automatically Generate Model Objects from JSON Files ⚑ 1. Guide ?? 1.1. Features ?? 1.1.1. From 1.1.2. To 1.2. Getting Starte

KATO, Shinya / εŠ θ—€ 真也 14 Nov 9, 2022
Create a Flutter User Profile Page UI where you can access and edit your user's information within your Flutter app.

Flutter Tutorial - User Profile Page UI 1/2 Create a Flutter User Profile Page UI where you can access and edit your user's information within your Fl

Johannes Milke 46 Dec 6, 2022
Create a Flutter User Profile Page UI where you can access and edit your user's information within your Flutter app.

Flutter Tutorial - User Profile Page UI #2 Create a Flutter User Profile Page UI where you can access and edit your user's information within your Flu

Johannes Milke 45 Dec 15, 2022
A responsive scaffold widget that adjusts to your device size, for your flutter mobile and web apps.

scaffold_responsive A responsive scaffold widget that adjusts to your device size, for your flutter mobile and web apps. Check out the Live demo here

Tushar Sadhwani 9 Sep 27, 2022