dartenv
Dart package to load environment variables to .env quickly and conveniently.
The usage is inspired by the dotenv package in Node.js.
Install
# recommened install
dart pub add dartenv --dev
Usage
Create a .env file in the root directory
MY_TEXT="Hello, World!"
ADMIN_PASSWORD="himom"
Make sure the dartenv package is imported in your application. If not, import it.
import 'package:dartenv/dartenv.dart';
void main() {
print(env('MY_TEXT')); // remove this after work
}
It's that simple. The Dartenv package retrieves the value of the specified key from the .env file with an env function.
import 'package:dartenv/dartenv.dart';
void checkPassword(String password) {
if (password != env('ADMIN_PASSWORD')) {
// ...
}
}
Multi values
Dartenv also allows you to use multiline values.
KEY="---------------------
...
--------------------------"
You can also use the \n character.
KEY="---------------------\n...\n--------------------------"
Comments
You can use comments in a line or inline with the # character.
# DO NOT EDIT THIS FILE
KEY="..."
NAME="JOHN DOE" # name of the author
Work System
The env function actually includes an optional path argument.
With this function, if specified, if the .env file in the specified path is not specified, the .env file in the root directory is read and the key in its first argument is searched for the .env file. Returns its value if found, returns null if not found.
Examples
See example for see example usage
Changelog
See changelog for more information