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