22  Dart VM Modes

The Dart VM (Virtual Machine) can run in different modes, and dart.vm.product and dart.vm.profile are boolean flags that indicate which mode the VM is currently running in.

22.0.1 The Three Main VM Modes

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   Debug Mode    │     │  Profile Mode   │     │  Release Mode   │
│                 │     │                 │     │   (Product)     │
├─────────────────┤     ├─────────────────┤     ├─────────────────┤
│ dart.vm.product │     │ dart.vm.product │     │ dart.vm.product │
│     = false     │     │     = false     │     │     = true      │
├─────────────────┤     ├─────────────────┤     ├─────────────────┤
│ dart.vm.profile │     │ dart.vm.profile │     │ dart.vm.profile │
│     = false     │     │     = true      │     │     = false     │
└─────────────────┘     └─────────────────┘     └─────────────────┘

22.0.2 Understanding Each Mode

Debug Mode (flutter run):

  • Used during development
  • Hot reload enabled
  • Assertions are enabled
  • Service extensions enabled
  • Debugging information available
  • Performance is slower

Profile Mode (flutter run --profile):

  • Used for performance profiling
  • Similar to release mode but with profiling information
  • Service extensions enabled for performance analysis
  • Better performance than debug mode

Release/Product Mode (flutter run --release or flutter build):

  • Used for production apps
  • Maximum optimization
  • Smaller app size
  • No debugging capabilities
  • Best performance

22.0.3 Practical Usage in Code

You can use these flags to conditionally execute code based on the build mode:

// Example: Different behavior based on build mode
if (const bool.fromEnvironment('dart.vm.product')) {
  // This code only runs in release/production mode
  print('Running in production mode');
} else {
  // This runs in debug or profile mode
  print('Running in development mode');
}

// More readable approach using kDebugMode
import 'package:flutter/foundation.dart';

if (kDebugMode) {
  // Debug mode only
  print('Debug: ${DateTime.now()}');
}

if (kProfileMode) {
  // Profile mode only
  print('Profiling performance...');
}

if (kReleaseMode) {
  // Release mode only
  print('Production ready!');
}

22.0.4 Common Use Cases

  1. API Endpoints:

    final apiUrl = const bool.fromEnvironment('dart.vm.product')
        ? 'https://api.production.com'
        : 'https://api.staging.com';
  2. Logging:

    void log(String message) {
      if (!const bool.fromEnvironment('dart.vm.product')) {
        print('[LOG] $message');
      }
    }
  3. Error Handling:

    try {
      // Some operation
    } catch (e) {
      if (kDebugMode) {
        print('Detailed error: $e');
      } else {
        // Send to crash reporting service
      }
    }

22.0.5 Key Takeaways for Your Learning

Given your Python and JavaScript background, think of these flags similar to:

  • Python’s __debug__ flag or NODE_ENV in JavaScript
  • They help you write code that behaves differently in development vs production
  • Flutter provides convenient constants (kDebugMode, kProfileMode, kReleaseMode) that are easier to use than the raw environment variables

This is particularly useful when you’re building apps that need different configurations for development (like using local servers) versus production (using real APIs).