D
DevOps & CI/CDMedium30 XP4 min read

How do you manage environment-specific configuration in Flutter (dev, staging, prod)?

TL;DR: Use --dart-define or --dart-define-from-file to inject environment variables at build time. Create a Config class that reads from String.fromEnvironment(). Avoid .env files at runtime — Flutter is compiled, not interpreted.

Full Answer

Flutter compiles to native code — there's no runtime .env file loading like Node.js. Environment configuration must be injected at build time via --dart-define flags.

--dart-define approach

Pass key=value pairs at build time: flutter run --dart-define=API_URL=https://api.dev.com. Read in Dart with String.fromEnvironment('API_URL'). These values are baked into the binary.

--dart-define-from-file (Flutter 3.7+)

Pass a JSON file with all env vars: flutter build apk --dart-define-from-file=env/prod.json. Cleaner than multiple --dart-define flags.

⚠️

--dart-define values are embedded in the compiled binary and can be extracted with tools. Never pass private API keys this way — use backend proxies for sensitive credentials.

Code Examples

dartConfig class with dart-define
Output
// Dev build: AppConfig.apiUrl = 'https://api.dev.com'
// Prod build: AppConfig.apiUrl = 'https://api.prod.com'
// Zero runtime overhead — values compiled in

Common Mistakes

  • Using dotenv packages at runtime — Flutter is compiled, .env files don't exist at runtime on device
  • Embedding API secrets in --dart-define — they can be extracted from the compiled binary

Interview Tip

💡

Explain that Flutter's build-time injection differs fundamentally from Node.js runtime env vars. This shows deep understanding of Flutter's compilation model.

#environment#dart-define#config#env-vars