0

I'm trying to do an env setup to flutter the project with null safety. but this error popped.

this worked well with the project without null safety.

source: https://stacksecrets.com/flutter/environment-configuration-in-flutter-app#:~:text=In%20order%20to%20load%20proper,which%20can%20set%20configuration%20dynamically.&text=Since%20the%20Environment%20class%20has,through%20out%20the%20application%20lifecycle.

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

  Environment._internal();
// here
// Non-nullable instance field 'config' must be initialized.
// Try adding an initializer expression, or add a field initializer in this constructor,or mark it 'late'

  static final Environment _singleton = Environment._internal();

  static const String DEV = 'DEV';
  static const String STAGING = 'STAGING';
  static const String PROD = 'PROD';

  BaseConfig config;

  initConfig(String environment) {
    config = _getConfig(environment);
  }

  BaseConfig _getConfig(String environment) {
    switch (environment) {
      case Environment.PROD:
        return ProdConfig();
      case Environment.STAGING:
        return StagingConfig();
      default:
        return DevConfig();
    }
  }
}
SULPHURIC ACID
  • 189
  • 1
  • 15
  • 1
    The error message told you what's wrong and how to fix it. `config` is declared with a non-nullable type, and therefore all constructors must initialize it in some manner. – jamesdlin Jan 03 '22 at 07:37

0 Answers0