initFromJson<T> static method

Future<void> initFromJson<T>({
  1. required EnvironmentType environmentType,
  2. required T fromJson(
    1. Map<String, dynamic> data
    ),
  3. DebugOptions debugOptions = const DebugOptions(),
})

Initialize the environment from file with JSON type in the assets folder.

Params: environmentType The environment type that you want to initialize the environment with. fromJson A function that takes a Map<String, dynamic> from environment file and returns an instance of the config class. debugOptions An optional parameter that allows you to specify the debug options for the environment.

Throws an Exception when Environment already initialized.

Implementation

static Future<void> initFromJson<T>({
  required EnvironmentType environmentType,
  required T Function(Map<String, dynamic> data) fromJson,
  DebugOptions debugOptions = const DebugOptions(),
}) async {
  if (_instance == null) {
    final environmentFileName = '${environmentType.toShortString()}.json';
    final environmentPath = 'res/config/$environmentFileName';

    final content = await rootBundle.loadString(environmentPath);
    final jsonObject = jsonDecode(content) as Map<String, dynamic>;

    _instance = Environment<T>._(
      environmentType: environmentType,
      debugOptions: debugOptions,
      config: fromJson(jsonObject),
    );
  } else {
    throw Exception('Environment already initialized');
  }
}