Skip to content

Load the configuration at runtime from a local.json file, making it accessible throughout the application.

License

Notifications You must be signed in to change notification settings

govarthananve/flutter_dot_json_env

Repository files navigation

flutter_dot_json_env

Pub Version

Load the configuration at runtime from a local.json file, making it accessible throughout the application.

About

This library is a fork of java-james/flutter_dotenv Dart library, initially modified to ensure compatibility with Flutter.

An environment comprises variables recognized by a process (such as PATH, PORT, etc.). During development (including testing and staging, etc.), it is beneficial to replicate the production environment by reading these values from a file.

This library parses the file and integrates its values with the built-in Platform.environment map.

Usage

1.In the root directory of your project, create a file named local.json with the following example content:

{
  "api_url": "https://api.local.dev.com",
  "foo": "foo",
  "bar": "bar"
}

2.Add the local.json file to your assets bundle in pubspec.yaml. Make sure the path matches the location of the local.json file!

assets:
  - local.json
  1. Load the local.json file in main.dart.
import 'package:flutter_dot_json_env/flutter_dot_json_env.dart';

Future main() async {
  await dotjsonenv.load(fileName: "local.json");
  //...runapp
}

4.Access Object key value from local.json throughout the application

import 'package:flutter_dot_json_env/flutter_dot_json_env.dart';

String varibale_name = dotjsonenv.env['key_value'] ?? 'default fallback value';

Using --dart-define to select the environtment JSON file

1.Create a file named [envFileName].json with the same object keys as shown in the following example content:

{
  "api_url": "https://api.local.dev.com",
  "foo": "foo",
  "bar": "bar"
}

2.Add the environment JSON file to your assets bundle in pubspec.yaml. Make sure the path matches the location of the local.json file!

assets:
  - assets/env/local.json
  - assets/env/dev.json
  - assets/env/test.json
  - assets/env/prelive.json
  - assets/env/live.json

3.Create new class for example Environment

class Environment {
  static const env = String.fromEnvironment('ENV');

  String get envFileName {
    switch (env) {
      case 'dev': // flutter run --dart-define=ENV=dev
        return 'assets/env/dev.json';
      case 'test': // flutter run --dart-define=ENV=test
        return 'assets/env/test.json';
      case 'prelive': // flutter run --dart-define=ENV=prelive
        return 'assets/env/prelive.json';
      case 'live': // flutter run --dart-define=ENV=live
        return 'assets/env/live.json';
      default: // flutter run
        return 'assets/env/local.json';
    }
  }

  static String get environmentName {
    return env.isNotEmpty ? env : "Environment name not found";
  }

  static String get apiUrl =>
      dotjsonenv.get('api_url', fallback: 'api url default fallback value');
  static String get foo =>
      dotjsonenv.env['foo'] ?? 'foo default fallback value';
  static String get bar =>
      dotjsonenv.env['bar'] ?? 'bar default fallback value';
}

4.Access value from Environment throughout the application

Future main() async {
  // await dotjsonenv.load(fileName: "assets/env/local.json"); // load as static json
  await dotjsonenv.load(fileName: Environment().envFileName);
  print('appURL::: ${Environment.apiUrl}'); // appURL::: https://api.local.com
  print('Foo::: ${Environment.foo}'); // Foo::: prelive foo
  print('Bar::: ${Environment.bar}'); // Bar::: prelive bar
//...runapp
}
  1. To run the app or during the build process
 flutter run --dart-define=ENV=<envFileName>
 // flutter run --dart-define=ENV=dev
 flutter build web --dart-define=ENV=<envFileName>
 // flutter build web --dart-define=ENV=dev

Null safety

To avoid null-safety checks for variables that are known to exist, there is a get() method that will throw an exception if the variable is undefined. You can also specify a default fallback value for when the variable is undefined in the .env file.

Future<void> main() async {
   await dotjsonenv.load(fileName: "local.json");

  String foo = dotjsonenv.get('key_value');

  // Or with fallback.
  String bar = dotjsonenv.get('missing_key_value', fallback: 'default fallback value');

  // This would return null.
  String? baz = dotenv.maybeGet('missing_key_value', fallback: null);
}

Discussion

Use the issue tracker for bug reports and feature requests.

Pull requests are welcome.

Prior art

license: MIT

About

Load the configuration at runtime from a local.json file, making it accessible throughout the application.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published