0 purchases
dartenv
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
copied to clipboard
Usage #
Create a .env file in the root directory
MY_TEXT="Hello, World!"
ADMIN_PASSWORD="himom"
copied to clipboard
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
}
copied to clipboard
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')) {
// ...
}
}
copied to clipboard
Multi values #
Dartenv also allows you to use multiline values.
KEY="---------------------
...
--------------------------"
copied to clipboard
You can also use the \n character.
KEY="---------------------\n...\n--------------------------"
copied to clipboard
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
copied to clipboard
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
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.