Sub topic 1
Launching and connecting DevTools
Flutter DevTools is a set of debugging and profiling tools that runs in a browser tab or inside your editor. It does not run inside your app. It talks to the app over a network connection, so most connection problems come down to which address DevTools is using, and whether the app behind that address is still running. This chapter covers the ways to start DevTools, how the connection works, and what to check when it fails.
Prerequisites
This chapter assumes you already have the following:
- Basic Dart syntax and async programming.
- Experience building simple Flutter apps with
StatelessWidgetandStatefulWidget. - A working Flutter install that can run an app on an emulator, simulator, browser, or physical device.
- Familiarity with the widget tree and the
buildmethod.
You do not need to have used DevTools before.
What DevTools is and what it connects to
DevTools is a web application. It ships with the Flutter SDK and the Dart SDK, so there is nothing extra to install. It has one tab per tool: Inspector, Performance, CPU Profiler, Memory, Debugger, Network, Logging, and App Size. Later chapters cover each tab.
DevTools never touches your app directly. Every Dart and Flutter app started in a debuggable mode runs on the Dart VM, and the VM exposes a VM service. The VM service is a local server that answers requests such as "list the isolates", "pause this isolate", or "give me the widget tree". DevTools connects to that server over HTTP and WebSocket and sends those requests.
The address of the VM service is the VM service URI. It looks like this:
http://127.0.0.1:51234/aBcDeFgHiJk=/The parts matter:
127.0.0.1:51234is the host and port. The port is chosen at random on each run unless you fix it.aBcDeFgHiJk=is an authentication code generated for that run. Without it, other programs on your machine cannot control your app.
Ways to launch DevTools
There are four common routes. They all end at the same connection, so choose whichever fits your workflow.
From the command line
Run your app, then read the output. Besides the "Syncing files" progress lines, flutter run prints the VM service address and, in most versions, a DevTools link:
# Run the app on a connected device
flutter run
# Typical lines in the output (the port and code differ on every run):
# A Dart VM Service on Pixel 8 is available at: http://127.0.0.1:51234/aBcDeFgHiJk=/
# The Flutter DevTools debugger and profiler on Pixel 8 is available at:
# http://127.0.0.1:9100?uri=http://127.0.0.1:51234/aBcDeFgHiJk=/The second link is a DevTools address with the VM service URI already placed in a uri query parameter. Paste it into a browser and DevTools connects at once. The terminal also lists keyboard shortcuts after startup. Press h to see the list for your Flutter version, which may include a shortcut that opens DevTools for you.
As a standalone server
You can start DevTools without any app, then connect to one afterward. This is how the spine's example does it:
# Run the app in profile mode on a connected device
flutter run --profile
# Start DevTools on its own, then connect using the VM service URI printed by flutter run
dart devtoolsdart devtools starts a local server and opens your browser. The page shows a "Connect to a running app" box. Paste the VM service URI printed by flutter run into that box and press Connect.
This route is useful when the app was started somewhere else, such as a CI machine, a teammate's session, or a separate terminal. It also works with dart run for command-line Dart programs.
From VS Code
With the Flutter and Dart extensions installed, start a debug session. Then open the command palette and run Dart: Open DevTools. VS Code offers to open DevTools in a browser or embedded in a panel inside the editor, and it supplies the VM service URI for you. During a debug session, the Flutter widget inspector and several other tools also appear as sidebar views and status-bar buttons.
From Android Studio or IntelliJ
Run your app from the IDE. In the Run or Debug tool window, use the Open DevTools toolbar button. The IDE launches the DevTools server and passes it the URI of the running session.
| Route | Needs a running app first? | You paste the URI? | Best for |
|---|---|---|---|
| Terminal link from `flutter run` | Yes | No | Quick checks from the command line |
| `dart devtools` | No | Yes | Attaching to an app started elsewhere |
| VS Code command | Yes (debug session) | No | Working with the editor open |
| Android Studio button | Yes (run session) | No | Working in a JetBrains IDE |
Connecting to an app you did not start from the IDE
You can attach to an app that is already running. This happens often with a physical device, a Flutter web app, or an app a colleague started. The steps are:
- Find the VM service URI in the output of the process that launched the app.
- Start DevTools with
dart devtools, or open an existing DevTools tab. - Paste the URI into the connect box and press Connect.
DevTools accepts the http:// form and the ws:// form of the address. If you only have the host and port, you must still supply the authentication code. A bare 127.0.0.1:51234 will be refused.
To attach a new flutter run session to an app that is already running, use flutter attach. It prints a fresh VM service URI for the existing process, and you can paste that into DevTools:
# Attach to an app that is already running on the connected device
flutter attachFixing the port
A random port is awkward if you reconnect often. You can ask Flutter to use a stable host port for the VM service:
flutter run --host-vmservice-port=8181The authentication code in the URI still changes each run, so a fixed port does not let you reuse a saved URI across runs. It only makes the port number predictable, which is useful for firewall rules or port forwarding.
Which mode you launch in changes what DevTools can do
DevTools reads the same VM service in every mode, but it cannot show what the app does not expose. A debug build includes debugging hooks and assertions. A profile build removes most of them so timing is realistic. A release build removes the VM service entirely.
| Mode | Command | VM service available | Use it for |
|---|---|---|---|
| Debug | `flutter run` | Yes, full | Inspector, debugger, logging |
| Profile | `flutter run --profile` | Yes, limited | Performance, CPU, memory |
| Release | `flutter run --release` | No | Final checks, size and speed on end-user builds |
Two practical consequences follow. First, you cannot attach DevTools to a release build, so no URI is printed. Second, in profile mode the Debugger tab and some Inspector features are unavailable, because the code to support them was removed to keep timing realistic. The next chapter explains the mode decision in depth. For now, confirm which mode you are in with a short check at startup:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
// kDebugMode, kProfileMode and kReleaseMode are compile-time constants.
final mode = kReleaseMode
? 'release'
: kProfileMode
? 'profile'
: 'debug';
debugPrint('Running in $mode mode');
runApp(const MaterialApp(home: Scaffold(body: Center(child: Text('Hello')))));
}debugPrint writes to the console, so you can read the line in the terminal output. It also appears in the Logging tab of DevTools, covered in a later chapter.
Connecting to different targets
The target platform affects how the URI reaches your machine.
- Emulator or simulator. The app and DevTools share a machine, so the URI usually works as printed.
- Physical Android device. Flutter forwards a device port to a port on your computer, using
adb. The URI points to the forwarded host port. If you unplug the cable, the forwarding stops and DevTools loses the connection. - Physical iOS device. Flutter uses a similar forwarding mechanism. On wireless debugging, the device and computer must be on the same network, and the app may need local-network permission on the device.
- Flutter web.
flutter run -d chromeprints a VM service URI too. DevTools works, but some tools, such as the CPU Profiler, behave differently or are missing, because the web runtime is not the Dart VM. - Desktop. The app runs natively on your machine, so the URI works as printed.
Common connection problems
Most failures fall into a few patterns.
- "Failed to connect" or an immediately closed connection. The URI is stale. The app was stopped or restarted. Copy the newest URI from the terminal.
- The page opens but stays on the connect screen. The
uriparameter is missing, or the authentication code was cut off when you copied it. Include the trailing/and the=characters. - A DevTools tab is greyed out. The tool is unavailable in the current mode. For example, the Debugger is disabled in profile mode. Relaunch in the right mode instead of searching for a setting.
- DevTools connects but shows no isolates or empty data. The app may be paused at a breakpoint or still starting. Wait for startup to finish, or resume execution.
- Connection works, then drops on a device. The cable, the Wi-Fi network, or an aggressive power-saving mode on the device broke the port forwarding. Reconnect with
flutter attachand use the new URI. - A second DevTools window shows the old app. Each window holds its own connection. Close the old one, or use the connect box to switch to the new URI.
Checking the connection is healthy
After connecting, take thirty seconds to confirm that DevTools sees your app before you start investigating.
- Look at the top bar. It shows the connected device and app name. If it shows the wrong device, you are attached to a different run.
- Open the Inspector tab. The widget tree should show your own root widget, such as
MaterialApp, beneath the framework widgets. If the tree is empty, you are probably attached to a non-Flutter Dart process. - Trigger something in the app, such as tapping a button that calls
debugPrint. Switch to the Logging tab and confirm the line appears. - Check the connection status in the corner. A disconnect banner appears if the app exits.
If all four checks pass, the connection is working and any odd results later come from the app, not from DevTools.
Summary of the working model
- DevTools is a client. The Dart VM's VM service is the server. The VM service URI joins them.
- The URI includes a per-run authentication code, so it changes whenever you start a fresh process.
- You can start DevTools from the terminal link,
dart devtools, VS Code, or Android Studio. All of them end at the same connection. - Debug and profile builds expose the VM service. Release builds do not.
- Most problems come from stale URIs, truncated URIs, a mode that disables the tool you want, or a broken device connection.
Exercise
You started flutter run --profile in one terminal and a teammate asks you to inspect the app from another machine. You run dart devtools, paste http://127.0.0.1:51234 into the connect box, and the connection is refused. Name two likely reasons and how you would fix each.
Show answer
First, the URI is incomplete. A VM service URI needs the authentication code, such as /aBcDeFgHiJk=/, and without it the VM service refuses the connection. Copy the complete URI printed by flutter run. Second, the address 127.0.0.1 refers to the machine running the app, so another machine cannot reach it. Run DevTools on the same machine as the app, or forward the port and use an address that resolves to it. A third possibility is that the app was restarted and the URI is stale. In that case, copy the newest URI from the terminal.
Exercise
You launch an app with flutter run --release and look for the VM service URI in the output, but none is printed. Why, and which command would you use instead to investigate a performance problem?
Show answer
Release builds remove the VM service, so DevTools has nothing to connect to and no URI is printed. Use flutter run --profile instead. Profile mode keeps the VM service available and still gives realistic performance timing.