Sub topic 4
Setting up your editor
This chapter assumes you have already installed the Flutter SDK and that flutter doctor runs without errors. It also assumes you know how to open a folder in an editor and edit JSON. Flutter does not require a specific editor. The command line tools do the real work, and the editor adds a layer on top. This chapter covers the two editors the Flutter documentation recommends, Visual Studio Code and Android Studio (or another IntelliJ-based IDE), and the settings that matter most.
What the editor adds
You can write Flutter apps in any text editor and run flutter run in a terminal. A configured editor gives you four extra things:
- Code analysis. Errors and warnings appear as you type, powered by the Dart analysis server.
- Code assistance. You get completion, quick fixes, refactorings such as "Wrap with Center", and snippets.
- Run and debug integration. You can start the app, set breakpoints, and trigger hot reload from the editor.
- Flutter-specific views. These include the widget inspector and a launcher for DevTools, the browser-based profiling and debugging suite.
All of these come from two pieces: the Dart plugin and the Flutter plugin. The Dart plugin handles the language. The Flutter plugin adds the framework features and depends on the Dart plugin. Installing the Flutter plugin installs both.
The diagram shows why a wrong SDK path breaks everything at once. Both the analysis server and the flutter tool come from the SDK folder that the plugin locates.
Setting up Visual Studio Code
- Install VS Code from the official site.
- Open the Extensions view (
Ctrl+Shift+X, orCmd+Shift+Xon macOS). - Search for Flutter and install the extension published by Dart Code. The Dart extension installs with it.
- Open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P) and run Flutter: Run Flutter Doctor. The output appears in a panel and should match what you saw in the terminal.
If the extension cannot find the SDK, it offers to locate it or download one. Pick the folder you installed earlier. You can also set the path in settings, as shown below.
Useful commands
Open the Command Palette and type "Flutter" or "Dart" to see the full list. These are the ones you will use first:
| Command | What it does |
|---|---|
| Flutter: New Project | Runs the equivalent of `flutter create` through a wizard |
| Flutter: Select Device | Picks the target for the next run |
| Flutter: Hot Reload | Injects changed code into the running app |
| Flutter: Hot Restart | Restarts the app and resets its state |
| Dart: Open DevTools | Opens the inspector, profiler, and logging tools |
Settings worth changing
VS Code stores workspace settings in .vscode/settings.json. This example sets the SDK path explicitly and turns on formatting when you save:
{
"dart.flutterSdkPath": "/home/you/development/flutter",
"editor.formatOnSave": true,
"[dart]": {
"editor.rulers": [80],
"editor.selectionHighlight": false,
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.suggestSelection": "first",
"editor.tabCompletion": "onlySnippets",
"editor.wordBasedSuggestions": "off"
}
}Replace the path with your own SDK location. The [dart] block applies only to Dart files. It follows the settings the Dart Code project recommends for Dart. In particular, turning off word-based suggestions stops the editor from offering random words from the file instead of real symbols.
The extension can also run hot reload whenever you save a file. The setting is dart.hotReloadOnSave. Its values range from never to all, and the "if dirty" variants reload only when the file has unsaved changes. Check the current options in the extension's settings page, because the defaults have changed between versions.
Launch configurations
Pressing F5 starts the app in debug mode. To run in profile or release mode from the editor, create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "my_first_app (debug)",
"request": "launch",
"type": "dart"
},
{
"name": "my_first_app (profile)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "my_first_app (release)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}Each entry shows up in the Run and Debug dropdown. Build modes are covered in a later chapter. For now, note that this file is where you pick one.
Setting up Android Studio and other IntelliJ IDEs
Android Studio is the better choice if you will work on the Android side of the project, because it includes the Android SDK manager and the emulator manager. Other JetBrains IDEs, such as IntelliJ IDEA, work the same way for the plugin.
- Open Settings (or Preferences on macOS) and go to Plugins.
- Search the Marketplace for Flutter and install it. Accept the prompt to install the Dart plugin as well.
- Restart the IDE.
- Under Languages & Frameworks > Flutter, confirm that the Flutter SDK path points to your SDK folder.
After the restart, New Flutter Project appears on the welcome screen. A toolbar at the top lets you choose a device and press Run or Debug. The Flutter Inspector and Flutter Outline panels are available in the tool window bar.
Android Studio can also create and start Android virtual devices through Device Manager. That is covered in the chapter on run targets.
Features that save time
These features work in both editors, though the shortcuts differ.
- Quick fixes and refactorings. Put the cursor on a widget and press
Ctrl+.in VS Code orAlt+Enterin Android Studio. You can wrap the widget inPadding,Center,Column, orRow, extract it into a new widget, or remove it and keep its child. - Snippets. In VS Code, type
stlessorstfuland accept the completion to get a fullStatelessWidgetorStatefulWidgetskeleton. Android Studio has equivalent live templates with the same names. - Go to definition. Hold
Ctrl(orCmd) and click a symbol to read the framework source. Reading howScaffoldorTextis built is one of the fastest ways to learn Flutter. - Formatting. Run
dart format .in a terminal or use the editor's format command. Flutter code has deeply nested trailing commas, and the formatter decides how they are laid out. Add a trailing comma after the last argument of a multi-line widget call and the formatter keeps one argument per line.
Configuring the analyzer
A new project contains analysis_options.yaml. It controls which warnings the analyzer reports, and both editors read it. The generated file includes the recommended lint rule set:
include: package:flutter_lints/flutter.yaml
linter:
rules:
prefer_const_constructors: true
avoid_print: trueThe include line pulls in the flutter_lints rules, which come from a package listed in pubspec.yaml. The rules map then turns individual rules on or off. Here prefer_const_constructors marks widgets that could be const, and avoid_print flags print calls left in the code. Warnings show as underlines in the editor and as output from flutter analyze in the terminal. If the editor shows no underlines at all, the analysis server may not be running. Check the Dart analysis status in the status bar.
Common editor problems
- "Flutter SDK not found" or no completion. The plugin cannot locate the SDK. Set the path in the plugin settings, or add the SDK's
binfolder to yourPATHand restart the editor, because editors readPATHonly when they start. - Red underlines on every import. Dependencies are not downloaded. Run
flutter pub get, or savepubspec.yamlso the editor runs it for you. - Stale errors after a branch switch or SDK upgrade. Restart the analysis server. In VS Code, run Dart: Restart Analysis Server. In Android Studio, use File > Invalidate Caches as a last resort.
- No device in the picker. The editor reads the same list as
flutter devices. If the terminal shows no devices, fix that first.
Exercise
You installed the Flutter extension in VS Code, but the editor shows no code completion and reports that the SDK is missing, even though flutter doctor works in your terminal. Name two things you could check or change.
Show answer
First, set the dart.flutterSdkPath setting (or use the extension's SDK locator) to your SDK folder. Second, check that the SDK's bin directory is on the PATH that the editor inherited. The editor reads PATH at startup, so restart it after changing PATH. Restarting the analysis server is a third option if the SDK path is already correct.
Exercise
Which file in a generated project controls the lint rules that your editor shows as warnings, and which line pulls in the recommended Flutter rule set?
Show answer
The file is analysis_options.yaml. The line include: package:flutter_lints/flutter.yaml pulls in the recommended rule set.