Skip to content

Commit 69138ac

Browse files
committed
refactor(config): enhance AppConfig with environment-aware configurations
- Add production() factory constructor with compile-time BASE_URL - Update development() to use localhost or compile-time BASE_URL - Remove unused demo() constructor - Enhance main.dart to support environment-based app initialization
1 parent 59b4e65 commit 69138ac

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

lib/app/config/app_config.dart

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
1+
12
import '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.
39
class 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+
}

lib/main.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import 'package:flutter/material.dart';
55
import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/config.dart';
66
import 'package:flutter_news_app_web_dashboard_full_source_code/bootstrap.dart';
77

8-
// Define the current application environment (production/development/demo).
9-
const AppEnvironment appEnvironment = AppEnvironment.demo;
8+
// Determine the current application environment from compile-time variables.
9+
// Defaults to 'demo' if no environment is specified.
10+
const AppEnvironment appEnvironment =
11+
String.fromEnvironment('APP_ENVIRONMENT') == 'production'
12+
? AppEnvironment.production
13+
: (String.fromEnvironment('APP_ENVIRONMENT') == 'development'
14+
? AppEnvironment.development
15+
: AppEnvironment.demo);
1016

1117
@JS('removeSplashFromWeb')
1218
external void removeSplashFromWeb();

0 commit comments

Comments
 (0)