Question #130EasyFlutter Basics

What is Flutter Daemon ?

#flutter

Answer

Overview

Flutter Daemon is a long-running background service that powers communication between development tools (IDEs, command-line tools) and the Flutter framework. It enables features like hot reload, device detection, and app launching.


What is Flutter Daemon?

The daemon is a JSON-based protocol that runs in the background, allowing tools to:

  • Detect connected devices
  • Launch Flutter apps
  • Perform hot reload/restart
  • Stream app logs
  • Monitor app state

How It Works

text
IDE (VS Code)
    ↓ (JSON-RPC)
Flutter Daemon
Flutter Tools
Running App

Starting the Daemon

bash
# Start Flutter daemon manually
flutter daemon

# Output (JSON events)
[{"event":"daemon.connected","params":{"version":"1.0.0"}}]

Note: IDEs (VS Code, Android Studio) start the daemon automatically when you run/debug Flutter apps.


Daemon Events

1. Device Events

Notifies when devices are connected/disconnected.

json
{
  "event": "device.added",
  "params": {
    "id": "emulator-5554",
    "name": "Android Emulator",
    "platform": "android",
    "emulator": true
  }
}

2. App Events

Tracks app lifecycle (starting, stopping, logs).

json
{
  "event": "app.start",
  "params": {
    "appId": "12345",
    "deviceId": "emulator-5554",
    "mode": "debug"
  }
}

3. Log Events

Streams app logs to IDE.

json
{
  "event": "app.log",
  "params": {
    "appId": "12345",
    "log": "I/flutter (12345): Hello from Flutter!"
  }
}

Daemon Commands

List Devices

bash
# Send command
{"id":1,"method":"device.getDevices"}

# Response
{
  "id": 1,
  "result": [
    {
      "id": "emulator-5554",
      "name": "Android Emulator",
      "platform": "android"
    }
  ]
}

Launch App

bash
{"id":2,"method":"app.start","params":{"deviceId":"emulator-5554","projectDirectory":"/path/to/project"}}

Hot Reload

bash
{"id":3,"method":"app.restart","params":{"appId":"12345","fullRestart":false}}

Hot Restart

bash
{"id":4,"method":"app.restart","params":{"appId":"12345","fullRestart":true}}

Stop App

bash
{"id":5,"method":"app.stop","params":{"appId":"12345"}}

Use Cases

1. IDE Integration

IDEs use the daemon to:

  • Detect devices in device selector
  • Launch/debug apps
  • Trigger hot reload on save
  • Display console logs

Example (VS Code):

  • When you press F5 (Run), VS Code sends
    text
    app.start
    to daemon
  • When you save a file, VS Code sends
    text
    app.restart
    (hot reload)

2. Custom Development Tools

Build custom tools that interact with Flutter apps.

Example: Custom Hot Reload Script

python
import subprocess
import json

# Start daemon
process = subprocess.Popen(
    ['flutter', 'daemon'],
    stdout=subprocess.PIPE,
    stdin=subprocess.PIPE,
    text=True
)

# Send hot reload command
command = {
    "id": 1,
    "method": "app.restart",
    "params": {"appId": "12345", "fullRestart": False}
}

process.stdin.write(json.dumps(command) + '
')
process.stdin.flush()

# Read response
response = process.stdout.readline()
print(response)

3. CI/CD Pipelines

Use daemon to run tests on connected devices.

bash
# Start daemon in background
flutter daemon &

# Launch app on device
# Send JSON command via stdin

# Run tests
flutter test

Daemon Architecture

text
┌──────────────────────────────────────┐
│  IDE (VS Code, Android Studio)       │
│  - Device selector                   │
│  - Run/Debug buttons                 │
│  - Hot reload on save                │
└──────────────────────────────────────┘
              ↓ JSON-RPC
┌──────────────────────────────────────┐
│  Flutter Daemon                      │
│  - Manages app lifecycle             │
│  - Streams logs                      │
│  - Device detection                  │
└──────────────────────────────────────┘
┌──────────────────────────────────────┐
│  Flutter Tools (flutter run, etc.)   │
└──────────────────────────────────────┘
┌──────────────────────────────────────┐
│  Running Flutter App                 │
└──────────────────────────────────────┘

Debugging the Daemon

View Daemon Logs

bash
# Run with verbose logging
flutter daemon --verbose

# Output shows all JSON events
[{"event":"daemon.connected","params":{"version":"1.0.0"}}]
[{"event":"device.added","params":{"id":"emulator-5554"}}]

Check Daemon Process

bash
# Check if daemon is running
ps aux | grep 'flutter daemon'

# Kill daemon
pkill -f 'flutter daemon'

Common Issues

1. Daemon Not Starting

bash
# Check Flutter installation
flutter doctor

# Restart daemon
pkill -f 'flutter daemon'
flutter daemon

2. Hot Reload Not Working

  • Daemon may have lost connection
  • Restart IDE or run
    text
    flutter run
    manually

3. Devices Not Showing

bash
# List devices manually
flutter devices

# Ensure daemon is running
flutter daemon

Daemon vs Flutter Run

Feature
text
flutter run
text
flutter daemon
PurposeRun app directlyBackground service for IDEs
InteractionCommand-lineJSON-RPC
Hot reloadManual (
text
r
key)
Automatic (IDE triggers)
LogsPrinted to consoleStreamed to IDE
Use caseManual developmentIDE integration

JSON-RPC Protocol

Request Format

json
{
  "id": 1,                  // Unique request ID
  "method": "app.start",    // Method name
  "params": {               // Method parameters
    "deviceId": "emulator-5554",
    "projectDirectory": "/path/to/project"
  }
}

Response Format

json
{
  "id": 1,                  // Matches request ID
  "result": {               // Result data
    "appId": "12345"
  }
}

Event Format

json
{
  "event": "device.added",  // Event type
  "params": {               // Event data
    "id": "emulator-5554",
    "name": "Android Emulator"
  }
}

Advanced Features

1. Streaming Logs

json
{
  "event": "app.log",
  "params": {
    "appId": "12345",
    "log": "I/flutter: Log message",
    "level": "info"
  }
}

2. Progress Events

json
{
  "event": "app.progress",
  "params": {
    "appId": "12345",
    "message": "Building...",
    "finished": false
  }
}

3. Debugger Attach

json
{
  "method": "app.callServiceExtension",
  "params": {
    "appId": "12345",
    "methodName": "ext.flutter.debugPaint"
  }
}

Best Practices

bash
# ✅ Let IDE manage daemon (don't run manually unless needed)
# VS Code/Android Studio automatically starts daemon

# ✅ Use flutter run for manual testing
flutter run

# ✅ Kill daemon if issues occur
pkill -f 'flutter daemon'

# ❌ Don't run multiple daemons simultaneously
# Can cause conflicts

Summary

FeatureDescription
PurposeBackground service for IDE integration
ProtocolJSON-RPC (request/response + events)
FeaturesDevice detection, app launching, hot reload, logs
Used byVS Code, Android Studio, custom tools
Start
text
flutter daemon
(auto-started by IDEs)

Key takeaway: Flutter Daemon powers the seamless development experience in IDEs, enabling hot reload, device detection, and real-time logging.

Learn more: Flutter Daemon Protocol