2021-03-24 13:24:40 +07:00
|
|
|
# Dart Configurable Environment
|
2021-03-24 16:39:10 +07:00
|
|
|
|
2021-03-24 17:20:28 +07:00
|
|
|
[![Pub](https://img.shields.io/pub/v/configurable.svg)](https://pub.dartlang.org/packages/configurable)
|
|
|
|
|
2021-03-24 14:21:36 +07:00
|
|
|
- [x] Allow to get property from env file
|
|
|
|
- [x] Cache property for runtime
|
|
|
|
- [x] Dotenv file support (use DotenvConfigurationProvider)
|
2021-03-24 16:39:10 +07:00
|
|
|
- [x] Support functions (getConfig, getConfigOrNull, hasConfigKey, setConfig)
|
2021-03-24 14:21:36 +07:00
|
|
|
- [x] Support nullsafety (dart 2.12.2+)
|
2021-03-24 16:39:10 +07:00
|
|
|
- [x] Custom configuration provider
|
2021-05-14 15:52:57 +07:00
|
|
|
- [x] Add Async functions support
|
2021-03-24 14:21:36 +07:00
|
|
|
|
2021-03-24 21:20:06 +07:00
|
|
|
# Issue
|
2021-05-14 15:52:57 +07:00
|
|
|
- Flutter app not support for dotenv (.env) when run on devices (because of runtime in device)
|
|
|
|
|
|
|
|
# Resolution
|
|
|
|
- Flutter app can be use persistence storage like SharedPreference or FlutterSecureStorage to implement the configuration provider
|
2021-03-24 21:20:06 +07:00
|
|
|
|
2021-03-24 14:22:56 +07:00
|
|
|
# Example
|
2021-03-24 16:39:10 +07:00
|
|
|
|
2021-03-24 14:22:56 +07:00
|
|
|
```dart
|
2021-03-24 16:39:10 +07:00
|
|
|
import 'package:configurable/configurable.dart';
|
2021-03-24 14:22:56 +07:00
|
|
|
import 'package:configurable/dotenv_configuration_provider.dart';
|
|
|
|
import 'package:configurable/simple_configuration_provider.dart';
|
|
|
|
import 'package:configurable/system_config.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
var key = 'app.name';
|
|
|
|
var value = 'CUBETIQ Solution';
|
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
/// in-memory provider (built-in)
|
2021-03-24 14:22:56 +07:00
|
|
|
var simpleProvider = SimpleConfigurationProvider();
|
|
|
|
SystemConfig.setProvider(simpleProvider);
|
|
|
|
var result1 = SystemConfig.getOrNull(key, defaultValue: value);
|
2021-03-24 16:39:10 +07:00
|
|
|
|
|
|
|
/// output: CUBETIQ Solution
|
2021-03-24 14:22:56 +07:00
|
|
|
print(result1);
|
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
/// get config functions
|
|
|
|
getConfig('app.title', defaultValue: 'My App Title!');
|
|
|
|
getConfigOrNull('app.null.ignore');
|
|
|
|
|
|
|
|
/// set config functions
|
|
|
|
/// support only mutable configuration provider
|
|
|
|
setConfig('my.app', 'Hello My App');
|
|
|
|
|
|
|
|
/// dotenv provider (from file .env)
|
2021-03-24 14:22:56 +07:00
|
|
|
var dotenvProvider = DotenvConfigurationProvider();
|
|
|
|
SystemConfig.setProvider(dotenvProvider);
|
|
|
|
var result2 = SystemConfig.getOrNull('HOME');
|
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
/// output: user's home directory
|
2021-03-24 14:22:56 +07:00
|
|
|
print(result2);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
# Implement custom configuration provider
|
|
|
|
|
|
|
|
```dart
|
|
|
|
class MyCustomProvider implements ConfigurationProvider {
|
|
|
|
Map<String, String?> configDataSet = {};
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool containsKey(String key) {
|
|
|
|
return configDataSet.containsKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String? getOrNull(String key, {String? defaultValue}) {
|
|
|
|
var value = configDataSet[key];
|
|
|
|
|
|
|
|
if (value == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-03-24 14:21:36 +07:00
|
|
|
# Contributors
|
2021-03-24 16:39:10 +07:00
|
|
|
|
|
|
|
- Sambo Chea <sombochea@cubetiqs.com>
|
|
|
|
|
|
|
|
# License
|
|
|
|
|
|
|
|
```text
|
|
|
|
MIT License
|
|
|
|
|
|
|
|
Copyright (c) 2021 CUBETIQ Solution
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
```
|