a generator to create config class from json files that support many environments.
motivation
if you use a json file to config your applications, perphaps you need to write a dart class that contain all variables you define with their data types. when you need to manage nested objects is more complicated. even if you want to add or delete a value in your json, you need to modify your dart class.
if you want to manage environments you need to writte manually the file path for every enviroment you need.
json_config_generator wants to help you to have an easy process with the manage of config files and environments.
install
to use json_config_generator, you will need build_runner/code-generator setup.
first, install build_runner and json_config_generator by adding them to your pubspec.yaml
file:
pubspec.yaml
dependencies:
json_config_annotation: ^0.1.0
dev_dependencies:
build_runner:
json_config_generator: ^0.1.0
- build_runner, the tool to run code-generators
- json_config_generator, the code generator
- json_config_annotation, a package containing annotations for json_config_generator
how to use
to use this generator need to create an empty config dart class that begins with $
using the annotation configuration
defining the environments that you want to have. each environment has name
and path
, if have more that one, the generator create an enum
to environments. also you can specify the name of that enum
. by default it’s name is environment
. also, you need to add some imports.
config.dart
import 'package:json_config_annotation/json_config_annotation.dart';
import 'dart:convert';
import 'package:flutter/services.dart';
part 'config.g.dart'; //{dart file name}.g.dart
@configuration(
environmentenumname: 'env',
environments:[
environment(name:'dev', path:'assets/config/dev.json'),
environment(name:'prd', path:'assets/config/prd.json'),
],
)
class $config{}
suposing that have the next json file
dev.json
{
"base_url": "https://example.com",
"custom_class": {
"value_1": "dfgdfgdfgwqrrqwrqwrqweqwe324523b252dghfdhd",
"value_2": "6lez7aiaaaaaan6qzg2343c252bv66b7yn5m8m6"
},
"int_value": 3,
"double_value": 3.5,
"boolean_value": true,
"string_list": ["hello", "world"],
"int_list": [1, 23, 5],
"bool_list": [false, true, true],
"custom_list": [
{
"value_1": "hello"
},
{
"value_1": "world"
}
]
}
the generator creates
config.g.dart
enum env { dev, prd }
class config {
config._();
static final instance = config._();
late string baseurl;
late _customclass customclass;
late int intvalue;
late double doublevalue;
late bool booleanvalue;
late list<string> stringlist;
late list<int> intlist;
late list<bool> boollist;
late list<_customlist> customlist;
future<void> init(env env) async {
string path = '';
switch (env) {
case env.dev:
path = 'assets/config/dev.json';
break;
case env.prd:
path = 'assets/config/prd.json';
break;
}
final jsonstring = await rootbundle.loadstring(path);
final config = json.decode(jsonstring) as map<string, dynamic>;
baseurl = config['base_url'] as string;
customclass =
_customclass.fromjson(config['custom_class'] as map<string, dynamic>);
intvalue = config['int_value'] as int;
doublevalue = config['double_value'] as double;
booleanvalue = config['boolean_value'] as bool;
stringlist = (config['string_list'] as list).cast<string>();
intlist = (config['int_list'] as list).cast<int>();
boollist = (config['bool_list'] as list).cast<bool>();
customlist = _customlist.listfromjson(config['custom_list'] as list);
}
}
class _customclass {
const _customclass({required this.value1, required this.value2});
factory _customclass.fromjson(map<string, dynamic> customclass) =>
_customclass(
value1: customclass['value_1'] as string,
value2: customclass['value_2'] as string,
);
final string value1;
final string value2;
}
class _customlist {
const _customlist({required this.value1});
factory _customlist.fromjson(map<string, dynamic> customlist) => _customlist(
value1: customlist['value_1'] as string,
);
final string value1;
static list<_customlist> listfromjson(list data) =>
data.map((e) => _customlist.fromjson(e as map<string, dynamic>)).tolist();
}
to use the generated configuration you need to call init
method to initialize all values. could be called in main
defining the environment that you want to use.
main.dart
void main() async {
widgetsflutterbinding.ensureinitialized();
await config.instance.init(env.dev);
runapp(const myapp());
}
now can access to fields easy using the instance
config.instance.baseurl;
config.instance.customclass.value1;
supported data types
- string
- int
- double
- bool
- customclass
- list<string>
- list<int>
- list<double>
- list<bool>
- list<customclass>
also can create nested classes!
important
- do not support nullable values
- do not support dynamic values
run the generator
to run the generator use one of the next commands:
flutter pub run build_runner build
dart pub run build_runner build
considerations
can also create a config with only one environment
. if that so, the enum
do not be created and when call init
method, do not need to pass any argument.
it is important to known that the generator takes the first value in lists to known the type of the list, so be caferull
Comments are closed.