Automatically formatting code in VS Code
Install the Flutter
extension (see Editor setup) to get automatic formatting of code in VS Code.
To automatically format the code in the current source code window, right-click in the code window and select Format Document
. You can add a keyboard shortcut to this VS Code Preferences.
To automatically format code whenever you save a file, set the editor.formatOnSave
setting to true
.
Automatically formatting code in Android Studio and IntelliJ
Install the Dart
plugin (see Editor setup) to get automatic formatting of code in Android Studio and IntelliJ. To format your code in the current source code window:
In macOS, press Cmd + Option + L.
In Windows and Linux, press Ctrl + Alt + L.
Android Studio and IntelliJ also provide a checkbox named Format code on save on the Flutter page in Preferences on macOS or Settings on Windows and Linux. This option corrects formatting in the current file when you save it.
Automatically formatting code with the dart
command
To correct code formatting in the command line interface (CLI), run the dart format
command:
$ dart format path1 path2 [...]