You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

import 'app_config.dart';
/// Exception thrown when an invalid environment is provided to [ConfigLoader].
class InvalidEnvironmentException implements Exception {
/// The invalid environment that was provided.
final String environment;
/// Creates an [InvalidEnvironmentException] with the provided environment.
InvalidEnvironmentException(this.environment);
@override
String toString() {
return 'InvalidEnvironmentException: Invalid environment "$environment". '
'Valid environments are: dev, prod';
}
}
/// Loads application configuration based on the specified environment.
///
/// This class provides a factory method to load configuration for either
/// 'dev' or 'prod' environments. It throws [InvalidEnvironmentException]
/// if an invalid environment is provided.
class ConfigLoader {
/// Private constructor to prevent instantiation.
ConfigLoader._();
/// Loads configuration for the specified environment.
///
/// [environment] - The environment to load ('dev' or 'prod').
///
/// Returns an [AppConfig] instance for the specified environment.
///
/// Throws [InvalidEnvironmentException] if [environment] is not 'dev' or 'prod'.
static AppConfig load(String environment) {
switch (environment.toLowerCase()) {
case 'dev':
return const AppConfig(
apiBaseUrl: 'https://api-dev.example.com',
enableLogging: true,
);
case 'prod':
return const AppConfig(
apiBaseUrl: 'https://api.example.com',
enableLogging: false,
);
default:
throw InvalidEnvironmentException(environment);
}
}
}

Powered by TurnKey Linux.