flutter_environment_config 1.0.1
flutter_environment_config: ^1.0.1 copied to clipboard
Plugin that exposes environment variables to your Dart code in Flutter as well as to your native code in iOS and Android. Bring some 12 factor love to your Flutter apps.
Flutter Environment Config #
A powerful Flutter plugin that provides type-safe access to environment variables with automatic code generation. Bring some 12 factor love to your Flutter apps! ๐
Inspired by react-native-config
โจ Features #
- ๐ง Type-Safe Code Generation: Automatically generates type-safe getters for environment variables
- ๐ฑ Multi-Platform: Access variables in Dart, iOS (Swift/Objective-C), and Android (Kotlin/Java)
- ๏ฟฝ Multiple Environments: Support for dev, staging, prod configurations
- ๐งช Testing Support: Mock values for testing environments
- โก Zero Configuration: Works out of the box with intelligent defaults
๐ฆ Installation #
Add to your pubspec.yaml:
dependencies:
flutter_environment_config: ^x.y.z # the latest version
๐ Usage #
1. Create Environment Files #
Create environment files in the env/ folder:
my_app/
โโโ env/
โ โโโ .env.develop # Development environment
โ โโโ .env.staging # Staging environment
โ โโโ .env.production # Production environment
โโโ pubspec.yaml
Example env/.env.develop:
API_URL=https://dev-api.myapp.com
API_KEY=dev-key-123
ENABLE_ANALYTICS=false
DEBUG_MODE=true
MAX_RETRIES=3
TIMEOUT_SECONDS=30.5
Example env/.env.staging:
API_URL=https://staging-api.myapp.com
API_KEY=staging-key-789
ENABLE_ANALYTICS=true
DEBUG_MODE=false
MAX_RETRIES=4
TIMEOUT_SECONDS=45.0
Example env/.env.production:
API_URL=https://api.myapp.com
API_KEY=prod-key-456
ENABLE_ANALYTICS=true
DEBUG_MODE=false
MAX_RETRIES=5
TIMEOUT_SECONDS=60.0
2. Load Environment Variables #
import 'package:flutter_environment_config/flutter_environment_config.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Load environment variables
await FlutterEnvironmentConfig.loadEnvVariables();
runApp(MyApp());
}
3. Use in Your App #
class ApiService {
void makeRequest() {
// Direct access
final apiUrl = FlutterEnvironmentConfig.get('API_URL');
final apiKey = FlutterEnvironmentConfig.get('API_KEY');
// Type conversion helpers
final maxRetries = FlutterEnvironmentConfig.getInt('MAX_RETRIES', defaultValue: 3);
final enableAnalytics = FlutterEnvironmentConfig.getBool('ENABLE_ANALYTICS', defaultValue: false);
print('API URL: $apiUrl');
print('Max retries: $maxRetries');
}
}
๐ฑ Native Configuration #
For accessing environment variables in native Android and iOS code:
๐ Platform-Specific Setup Guides:
- ๐ฑ Android Setup Guide - Gradle configuration, build flavors, ProGuard setup
- ๐ iOS Setup Guide - Xcode schemes, Info.plist, Swift/Objective-C usage
โ๏ธ Code Generator #
Generate type-safe getters for your environment variables:
dart run flutter_environment_config:generate
This creates lib/generated/flutter_environment_config.g.dart with type-safe access:
// Auto-generated - DO NOT MODIFY
abstract class FlutterEnvironmentConfigGeneration {
// Type-safe getters
static String? get apiUrl => FlutterEnvironmentConfig.get('API_URL');
static String? get apiKey => FlutterEnvironmentConfig.get('API_KEY');
static bool? get enableAnalytics {
final value = FlutterEnvironmentConfig.get('ENABLE_ANALYTICS');
return value?.toLowerCase() == 'true';
}
static int? get maxRetries {
final value = FlutterEnvironmentConfig.get('MAX_RETRIES');
return value != null ? int.tryParse(value) : null;
}
// Constant keys
static const String kApiUrlKey = 'API_URL';
static const String kApiKeyKey = 'API_KEY';
}
Usage with Generated Code:
import 'lib/generated/flutter_environment_config.g.dart';
class ApiService {
void makeRequest() {
final apiUrl = FlutterEnvironmentConfigGeneration.apiUrl;
final maxRetries = FlutterEnvironmentConfigGeneration.maxRetries ?? 3;
}
}
Generator Configuration #
Customize output directory in pubspec.yaml:
flutter_environment_config:
output_dir: lib/environment # Default: lib/generated
๐งช Testing #
Mock environment variables for testing:
import 'package:flutter_environment_config/flutter_environment_config.dart';
void main() {
setUp(() {
FlutterEnvironmentConfig.loadValueForTesting({
'API_URL': 'https://test-api.com',
'DEBUG_MODE': 'true',
'MAX_RETRIES': '1',
});
});
test('should use test environment variables', () {
final apiUrl = FlutterEnvironmentConfig.get('API_URL');
expect(apiUrl, equals('https://test-api.com'));
});
}
โ ๏ธ Security Notice #
Environment variables are embedded in your app bundle and can be reverse-engineered. Never store sensitive data in .env files.
โ Never store:
- API secrets and private keys
- Database credentials
- Signing certificates
โ Safe to store:
- API endpoints and URLs
- Feature flags
- Debug settings
๐ค Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments #
- Inspired by react-native-config
- Built with โค๏ธ for the Flutter community