Getting LateInitializationError while configuring dotenv in Flutter

69 Views Asked by At

Scenario

I am trying to fetch APIs from an environment file in my flutter project. I have created files .env, .env.development, .env.production, and .env.staging in the root of my flutter project.

I have created a file setup.dart with the following code:

class Environment {
  factory Environment() {
    return _singleton;
  }

  Environment._internal();

  static final Environment _singleton = Environment._internal();

  static const String dev = 'dev';
  static const String stag = 'stag';
  static const String prod = 'prod';

  late BaseConfig config;

  initConfig(String environment) {
    config = getConfig(environment);
    log("config -> $config");
  }

  BaseConfig getConfig(String environment) {
    switch (environment) {
      case Environment.prod:
        return EnvConfig();
      case Environment.stag:
        return EnvConfig();
      case Environment.dev:
        return EnvConfig();
      default:
        return EnvConfig();
    }
  }

  static String getName(String environment) {
    switch (environment) {
      case Environment.prod:
        return '.env.production';
      case Environment.stag:
        return '.env.staging';
      case Environment.dev:
        return '.env.development';
      default:
        return '.env.development';
    }
  }
}

In the .env.development file I have two environment variables:

GET_ALL_COUNTRIES = https://<api-root>/api/v1/config/getAllCountries
GET_COUNTRY_DETAILS = https://<api-root>/api/v1/config/getCountryDetails

In a file config.dart I have a couple of getter String functions as follows:

class EnvConfig implements BaseConfig {
  @override
  String get getAllCountries {
    return dotenv.env['GET_ALL_COUNTRIES'] ?? "";
  }

  @override
  String get getCountryDetails {
    return dotenv.env['GET_COUNTRY_DETAILS'] ?? "";
  }
}

And in a file base.dart these are mapped as:

abstract class BaseConfig {
  String get getAllCountries;
  String get getCountryDetails;
}

I have made sure to add the environment files in my pubspec.yaml under assets.

Finally, I am calling the initEnvironment() function in MyApp which I have made stateful

@override
  void initState() {
    super.initState();
    initEnvironment();
  }

  Future<void> initEnvironment() async {
    try {
      // ? environment string
      String environment = const String.fromEnvironment(
        'ENVIRONMENT',
        defaultValue: Environment.dev,
      );

      // ? load environment file
      await dotenv.load(
        fileName:
            // '.env.development',
            Environment.getName(environment),
      );

      // ? initialize the config
      await Environment().initConfig(environment);
    } catch (_) {}
  }

Problem:

When running the project I am getting the following error:

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field 'config' has not been initialized. #0 Environment.config (package:investnation/environment/setup.dart)

Request:

I am unable to understand why this error is occurring, as I have made a similar implementation in a previous project. Please help me find the issue and fix it.

0

There are 0 best solutions below