global_configs

Last updated:

0 purchases

global_configs Image
global_configs Images
Add to Cart

Description:

global configs

Global Configs #


A flutter package to manage application configurations via singleton pattern.
Show some ❤️ and star the repo to support the project
Installation #
Add global_configs: ^1.1.1 to your pubspec.yaml dependencies. And import it:
import 'package:global_configs/global_configs.dart';
copied to clipboard
How to use #
Simply load your configurations and use them everywhere in your application.
Load configurations #
Before usgin configurations you need to load them from a Map or a JSON file.
Load from Map
If you consider to keep your configs in a dart file, create a file in project's directory like this:
/configs/dev.dart
final Map<String, dynamic> dev = {
"appearance": {
"defaultTheme": "Dark",
"templateScale": 1,
}
};
copied to clipboard
Then import the file at the top level of the project, and use loadFromMap function to simply load the configs.
import 'configs/dev.dart' as configs;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

runApp(MyApp());
}
copied to clipboard
Use pathoption to load configs into a specific path.
// Load configs from map
GlobalConfigs().loadFromMap(configs.dev, path: 'appearance.theme');
copied to clipboard
Load from JSON file
Create your JSON configuration file in your assets folder. for example: configs/dev.json
{
"config1": "test",
"config2": 1
}
copied to clipboard
Don't forget to update your pubspec.yaml file
flutter:
assets:
- configs/dev.json
copied to clipboard
Then use loadJsonFromdir function to load your JSON file
import 'package:global_configs/global_configs.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

// Load configs from json
await GlobalConfigs().loadJsonFromdir('configs/dev.json');

runApp(MyApp());
}
copied to clipboard
Use pathoption to load configs into a specific path.
// Load configs from json
await GlobalConfigs().loadJsonFromdir('configs/dev.json', path: 'appearance.theme');
copied to clipboard
Get configuration #
After loading configurations use get function to simply access to a configuration.
T get<T>(String path, {T Function(dynamic)? converter})
copied to clipboard
Arguments

path (String): The path of the config to get.
converter (T Function(dynamic)?): The function to cast the value to a custom type

Returns

<T>|null: Returns the resolved value or null if the config is not found.

Example
Map<String, dynamic> configs = {
'appearance': {
'defaultTheme': 'Dark',
'color': '0xFFB74093',
},
'size': 100,
};

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

var size = GlobalConfigs().get('size'); // 100
var defaultTheme = GlobalConfigs().get('appearance.defaultTheme'); // Dark
Color color = GlobalConfigs().get<Color>(
'appearance.color',
converter: (value) => Color(int.parse(value)),
);
copied to clipboard
Set configuration #
Use set function to update or add a new configuration.
void set<T>(String path, T value)
copied to clipboard
Arguments

path (String): The path of the config to set.
value (T): The value to set.

Example
Map<String, dynamic> configs = {
'appearance': {
'defaultTheme': 'Dark',
'color': 'red',
},
'size': 100,
};

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

GlobalConfigs().set('appearance.defaultTheme', 'Light'); // Light
copied to clipboard
Unset configuration #
Use unset function to remove a configuration.
void unset(String path)
copied to clipboard
Arguments

path (String): The path of the property to remove.

Example
Map<String, dynamic> configs = {
'appearance': {
'defaultTheme': 'Dark',
'color': 'red',
},
'size': 100,
};

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

var defaultTheme = GlobalConfigs().unset('appearance.defaultTheme'); // {'appearance': {'color': 'red'}, 'size': 100}
copied to clipboard
For more information see examples

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.