Last active
November 16, 2020 18:58
-
-
Save Zfinix/40df00b5d37a52b3c1a90963276c366b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| import '../config_loader.dart'; | |
| import '../env_config.dart'; | |
| class Config implements EnvConfig { | |
| Config._(); | |
| static Future<EnvConfig> create() async { | |
| // read secrets | |
| await ConfigLoader.initialize(); | |
| // return the new env Config | |
| return Config._(); | |
| } | |
| @override | |
| String get name => 'DEV'; | |
| @override | |
| String get hostURL => ConfigLoader.hostURL; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| import '../config_loader.dart'; | |
| import '../env_config.dart'; | |
| class Config implements EnvConfig { | |
| Config._(); | |
| static Future<EnvConfig> create() async { | |
| // read secrets | |
| await ConfigLoader.initialize(isDev: false); | |
| // return the new env Config | |
| return Config._(); | |
| } | |
| @override | |
| String get name => 'PROD'; | |
| @override | |
| String get hostURL => ConfigLoader.hostURL; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "devHost": "Development Host", | |
| "prodHost": "Production Host" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:convert'; | |
| import 'package:flutter/services.dart'; | |
| class ConfigLoader { | |
| static Map<String, dynamic> _config; | |
| static bool _isDev; | |
| static Future<void> initialize({bool isDev = true}) async { | |
| try { | |
| //Set Environment | |
| _isDev = isDev; | |
| //load rawConfig String | |
| final rawConfig = await rootBundle.loadString('config/app_config.json'); | |
| _config = json.decode(rawConfig) as Map<String, dynamic>; | |
| } catch (e) { | |
| print(e.toString()); | |
| } | |
| } | |
| static String get hostURL => | |
| _config[_isDev ? 'devHost' : 'prodHost'] as String; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import '../env_config.dart'; | |
| import '_dev.dart' as _dev; | |
| import '_prod.dart' as _prod; | |
| abstract class Environment { | |
| static Future<EnvConfig> get dev => _dev.Config.create(); | |
| static Future<EnvConfig> get prod => _prod.Config.create(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class EnvConfig { | |
| String get name; | |
| String get hostURL; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/foundation.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:socket_chat/config/env_config.dart'; | |
| import 'config/config_loader.dart'; | |
| void mainCommon(EnvConfig envConfig) async { | |
| print(ConfigLoader.hostURL); | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| // This widget is the root of your application. | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Socket Chat', | |
| home: Home(), | |
| debugShowCheckedModeBanner: false, | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<void> main() async { | |
| WidgetsFlutterBinding.ensureInitialized(); | |
| final envConfig = await Environment.dev; | |
| mainCommon(envConfig); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<void> main() async { | |
| WidgetsFlutterBinding.ensureInitialized(); | |
| final envConfig = await Environment.prod; | |
| mainCommon(envConfig); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment