Environment Variables
The app usesflutter_dotenv to load environment variables from a .env file.
Creating the .env File
Create a.env file in the project root:
Loading Environment Variables
The.env file is loaded at app startup in main.dart:
lib/main.dart:12-17
Accessing Environment Variables
Environment variables are accessed viadotenv.env:
lib/config/api_config.dart:16
API Configuration
TheApiConfig class in lib/config/api_config.dart manages API settings.
Backend API URL
lib/config/api_config.dart
Platform-Specific URLs
- Android Emulator
- Physical Device
- iOS Simulator
- Production
Use
10.0.2.2 to connect to the host machine’s localhost:Google Maps API Key
The Google Maps API key is required for map visualization and directions.Obtaining an API Key
- Go to Google Cloud Console
- Create a new project or select existing
- Enable these APIs:
- Maps SDK for Android
- Maps SDK for iOS
- Directions API
- Create credentials → API Key
- Restrict the key (recommended):
- Android: Add package name and SHA-1 fingerprint
- iOS: Add bundle identifier
Platform Configuration
Android
Add toandroid/app/src/main/AndroidManifest.xml:
iOS
Add toios/Runner/AppDelegate.swift:
Asset Configuration
Assets are configured inpubspec.yaml:
pubspec.yaml:75-84
This ensures the .env file is bundled with the app.
HTTP Client Configuration
TheApiService uses configurable timeouts:
lib/services/api_service.dart:41-42
Default timeout is 30 seconds (configured in ApiConfig.timeout).
Development vs Production
For different environments, consider using flavor-specific configuration:Development
Production
Security Best Practices
Environment Variables
-
Never commit
.envto version control -
Add to .gitignore:
-
Use example file for documentation:
API Key Security
- Restrict API keys in Google Cloud Console
- Use different keys for development and production
- Monitor usage in Google Cloud Console
- Rotate keys periodically
Configuration Checklist
- Create
.envfile withGOOGLE_DM_API_KEY - Add
.envto.gitignore - Configure
baseUrlinApiConfigfor your platform - Add Google Maps API key to
AndroidManifest.xml - Add Google Maps API key to
AppDelegate.swift - Enable required APIs in Google Cloud Console
- Verify backend server is running and accessible
- Test API connectivity with
flutter run
Troubleshooting
”Failed to load .env” Error
Ensure.env is listed in pubspec.yaml assets and run:
“Network error” or “Connection refused”
Check:- Backend server is running
- Correct
baseUrlfor your platform - Firewall allows connections
- Device/emulator has internet access
Google Maps Not Displaying
Verify:- API key is valid and unrestricted (during development)
- Maps SDK enabled in Google Cloud Console
- API key added to platform manifest files
- Billing enabled in Google Cloud (required for Maps API)