Last updated:
0 purchases
flutter dot json env
flutter_dot_json_env #
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"
}
copied to clipboard
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
copied to clipboard
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
}
copied to clipboard
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';
copied to clipboard
Useing --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"
}
copied to clipboard
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
copied to clipboard
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';
}
copied to clipboard
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
}
copied to clipboard
To run the app or while taking build
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
copied to clipboard
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);
}
copied to clipboard
Discussion #
Use the issue tracker for bug reports and feature requests.
Pull requests are welcome.
Prior art #
java-james/flutter_dotenv (dart)
license: MIT #
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.