|
1 | 1 | import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/app_environment.dart'; |
2 | 2 |
|
| 3 | +/// A class to hold all environment-specific configurations. |
| 4 | +/// |
| 5 | +/// This class is instantiated in `main.dart` based on the compile-time |
| 6 | +/// environment variable. It provides a type-safe way to access |
| 7 | +/// environment-specific values like API base URLs. |
3 | 8 | class AppConfig { |
4 | | - const AppConfig({required this.environment, required this.baseUrl}); |
| 9 | + /// Creates a new [AppConfig]. |
| 10 | + AppConfig({ |
| 11 | + required this.environment, |
| 12 | + required this.baseUrl, |
| 13 | + // Add other environment-specific configs here (e.g., analytics keys) |
| 14 | + }); |
5 | 15 |
|
6 | | - factory AppConfig.production() => const AppConfig( |
7 | | - environment: AppEnvironment.production, |
8 | | - baseUrl: 'http://api.yourproductiondomain.com', |
9 | | - ); |
| 16 | + /// A factory constructor for the production environment. |
| 17 | + /// |
| 18 | + /// Reads the `BASE_URL` from a compile-time variable. Throws an exception |
| 19 | + /// if the URL is not provided, ensuring a production build cannot proceed |
| 20 | + /// with a missing configuration. This is a critical safety check. |
| 21 | + factory AppConfig.production() { |
| 22 | + const baseUrl = String.fromEnvironment('BASE_URL'); |
| 23 | + if (baseUrl.isEmpty) { |
| 24 | + // This check is crucial for production builds. |
| 25 | + throw StateError( |
| 26 | + 'FATAL: The BASE_URL compile-time variable was not provided for this ' |
| 27 | + 'production build. Ensure the build command includes ' |
| 28 | + '--dart-define=BASE_URL=https://your.api.com', |
| 29 | + ); |
| 30 | + } |
| 31 | + return AppConfig(environment: AppEnvironment.production, baseUrl: baseUrl); |
| 32 | + } |
10 | 33 |
|
11 | | - factory AppConfig.demo() => |
12 | | - const AppConfig(environment: AppEnvironment.demo, baseUrl: ''); |
| 34 | + /// A factory constructor for the demo environment. |
| 35 | + factory AppConfig.demo() => AppConfig( |
| 36 | + environment: AppEnvironment.demo, |
| 37 | + baseUrl: '', // No API access needed for in-memory demo |
| 38 | + ); |
13 | 39 |
|
14 | | - factory AppConfig.development() => const AppConfig( |
| 40 | + /// A factory constructor for the development environment. |
| 41 | + factory AppConfig.development() => AppConfig( |
15 | 42 | environment: AppEnvironment.development, |
16 | | - baseUrl: 'http://localhost:8080', |
| 43 | + baseUrl: const String.fromEnvironment( |
| 44 | + 'BASE_URL', |
| 45 | + defaultValue: 'http://localhost:8080', |
| 46 | + ), |
17 | 47 | ); |
18 | 48 |
|
19 | 49 | final AppEnvironment environment; |
|
0 commit comments