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
'Running in production mode');
print(} else {
// This runs in debug or profile mode
'Running in development mode');
print(}
// More readable approach using kDebugMode
import 'package:flutter/foundation.dart';
if (kDebugMode) {
// Debug mode only
'Debug: ${DateTime.now()}');
print(}
if (kProfileMode) {
// Profile mode only
'Profiling performance...');
print(}
if (kReleaseMode) {
// Release mode only
'Production ready!');
print(}
22.0.4 Common Use Cases
API Endpoints:
final apiUrl = const bool.fromEnvironment('dart.vm.product') ? 'https://api.production.com' : 'https://api.staging.com';
Logging:
void log(String message) { if (!const bool.fromEnvironment('dart.vm.product')) { '[LOG] $message'); print(} }
Error Handling:
try { // Some operation } catch (e) { if (kDebugMode) { 'Detailed error: $e'); print(} 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 orNODE_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).