Environment Commands
The env
command group provides comprehensive environment variable management for Flutter projects. FDAWG stores environment variables in .environment
directory files and can generate Dart code for easy access in your Flutter app.
Overview
fdawg env [subcommand] [options] [arguments]
Environment files are stored in the .environment
directory in your Flutter project root. Each file represents a different environment (development, staging, production, etc.).
Available Subcommands
list
- List Environment Files
Lists all environment files in the .environment
directory.
fdawg env list
Example Output:
Available environment files:
- development (3 variables)
- staging (5 variables)
- production (4 variables)
show
- Display Environment Variables
Shows all variables in a specific environment file.
fdawg env show <env-name>
Parameters:
<env-name>
: Name of the environment file (without .env extension)
Example:
fdawg env show development
Example Output:
Environment: development
API_URL=https://dev-api.example.com
DEBUG_MODE=true
LOG_LEVEL=debug
create
- Create New Environment
Creates a new environment file, optionally copying from an existing environment.
fdawg env create <env-name> [--copy <source-env>]
Parameters:
<env-name>
: Name for the new environment file--copy, -c
: Copy variables from an existing environment file
Examples:
# Create empty environment
fdawg env create production
# Create by copying from development
fdawg env create staging --copy development
add
- Add/Update Variable
Adds a new variable or updates an existing one in an environment file.
fdawg env add <key> <value> [--env <env-name>]
Parameters:
<key>
: Variable key (must start with letter or underscore)<value>
: Variable value--env, -e
: Target environment file (default: development)
Examples:
# Add to default (development) environment
fdawg env add API_URL https://api.example.com
# Add to specific environment
fdawg env add DEBUG_MODE false --env production
# Add with spaces in value
fdawg env add APP_NAME "My Flutter App" --env staging
remove
- Remove Variable
Removes a variable from an environment file.
fdawg env remove <key> [--env <env-name>]
Parameters:
<key>
: Variable key to remove--env, -e
: Target environment file (default: development)
Examples:
# Remove from default environment
fdawg env remove DEBUG_MODE
# Remove from specific environment
fdawg env remove API_URL --env staging
delete
- Delete Environment File
Deletes an entire environment file.
fdawg env delete <env-name>
Parameters:
<env-name>
: Name of the environment file to delete
Example:
fdawg env delete staging
Note: This operation requires confirmation and cannot be undone.
generate-dart
- Generate Dart Code
Generates a Dart file with all environment variables for easy access in your Flutter app.
fdawg env generate-dart
This creates lib/generated/environment.dart
with a class containing all environment variables from all environment files.
Generated Code Example:
class Environment {
static const String apiUrl = String.fromEnvironment('API_URL', defaultValue: 'https://api.example.com');
static const String debugMode = String.fromEnvironment('DEBUG_MODE', defaultValue: 'true');
static const String logLevel = String.fromEnvironment('LOG_LEVEL', defaultValue: 'info');
}
File Structure
Environment files are stored in the .environment
directory:
your_flutter_project/
├── .environment/
│ ├── development.env
│ ├── staging.env
│ └── production.env
├── lib/
│ └── generated/
│ └── environment.dart # Generated by generate-dart
└── pubspec.yaml
Variable Naming Rules
- Must start with a letter (A-Z, a-z) or underscore (_)
- Can contain letters, numbers, and underscores
- Case-sensitive
- Cannot contain spaces or special characters
Valid Examples:
API_URL
debug_mode
_private_key
VERSION_1_0
Invalid Examples:
1_API_URL
(starts with number)API-URL
(contains hyphen)API URL
(contains space)
Best Practices
1. Environment Organization
# Development - for local development
fdawg env create development
fdawg env add API_URL https://dev-api.example.com --env development
fdawg env add DEBUG_MODE true --env development
# Staging - for testing
fdawg env create staging --copy development
fdawg env add API_URL https://staging-api.example.com --env staging
# Production - for release
fdawg env create production --copy staging
fdawg env add API_URL https://api.example.com --env production
fdawg env add DEBUG_MODE false --env production
2. Security Considerations
- Never commit sensitive data (API keys, passwords) to version control
- Add
.environment/
to your.gitignore
file - Use placeholder values in committed environment files
- Store real secrets in CI/CD environment variables
3. Using Generated Dart Code
After running generate-dart
, use environment variables in your Flutter app:
import 'package:your_app/generated/environment.dart';
void main() {
print('API URL: ${Environment.apiUrl}');
print('Debug Mode: ${Environment.debugMode}');
}
4. Build-time Configuration
Pass environment variables during Flutter build:
# Development build
flutter build apk --dart-define=API_URL=https://dev-api.example.com
# Production build
flutter build apk --dart-define=API_URL=https://api.example.com
Common Workflows
Setting up a new project
# Create development environment
fdawg env create development
fdawg env add API_URL https://dev-api.example.com --env development
fdawg env add DEBUG_MODE true --env development
# Create production environment
fdawg env create production
fdawg env add API_URL https://api.example.com --env production
fdawg env add DEBUG_MODE false --env production
# Generate Dart code
fdawg env generate-dart
Updating configuration
# Update API URL for staging
fdawg env add API_URL https://new-staging-api.example.com --env staging
# Regenerate Dart code
fdawg env generate-dart
Cleaning up
# Remove unused variable
fdawg env remove OLD_API_KEY --env development
# Delete unused environment
fdawg env delete old_staging
Next: Learn about Asset Commands for managing project assets.