Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions lib/app/config/app_config.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@

import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/app_environment.dart';

/// A class to hold all environment-specific configurations.
///
/// This class is instantiated in `main.dart` based on the compile-time
/// environment variable. It provides a type-safe way to access
/// environment-specific values like API base URLs.
class AppConfig {
const AppConfig({required this.environment, required this.baseUrl});
/// Creates a new [AppConfig].
AppConfig({
required this.environment,
required this.baseUrl,
// Add other environment-specific configs here (e.g., analytics keys)
});

factory AppConfig.production() => const AppConfig(
environment: AppEnvironment.production,
baseUrl: 'http://api.yourproductiondomain.com',
);
/// A factory constructor for the production environment.
///
/// Reads the `BASE_URL` from a compile-time variable. Throws an exception
/// if the URL is not provided, ensuring a production build cannot proceed
/// with a missing configuration. This is a critical safety check.
factory AppConfig.production() {
const baseUrl = String.fromEnvironment('BASE_URL');
if (baseUrl.isEmpty) {
// This check is crucial for production builds.
throw const FormatException(
'FATAL: The BASE_URL compile-time variable was not provided for this '
'production build. Ensure the build command includes '
'--dart-define=BASE_URL=https://your.api.com',
);
}
return AppConfig(environment: AppEnvironment.production, baseUrl: baseUrl);
}

factory AppConfig.demo() =>
const AppConfig(environment: AppEnvironment.demo, baseUrl: '');
/// A factory constructor for the demo environment.
factory AppConfig.demo() => AppConfig(
environment: AppEnvironment.demo,
baseUrl: '', // No API access needed for in-memory demo
);

factory AppConfig.development() => const AppConfig(
/// A factory constructor for the development environment.
factory AppConfig.development() => AppConfig(
environment: AppEnvironment.development,
baseUrl: 'http://localhost:8080',
baseUrl: const String.fromEnvironment(
'BASE_URL',
defaultValue: 'http://localhost:8080',
),
);

final AppEnvironment environment;
final String baseUrl;
}
}
10 changes: 8 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import 'package:flutter/material.dart';
import 'package:flutter_news_app_web_dashboard_full_source_code/app/config/config.dart';
import 'package:flutter_news_app_web_dashboard_full_source_code/bootstrap.dart';

// Define the current application environment (production/development/demo).
const AppEnvironment appEnvironment = AppEnvironment.demo;
// Determine the current application environment from compile-time variables.
// Defaults to 'demo' if no environment is specified.
const AppEnvironment appEnvironment =
String.fromEnvironment('APP_ENVIRONMENT') == 'production'
? AppEnvironment.production
: (String.fromEnvironment('APP_ENVIRONMENT') == 'development'
? AppEnvironment.development
: AppEnvironment.demo);

@JS('removeSplashFromWeb')
external void removeSplashFromWeb();
Expand Down
Loading