Configuration
Introduction
Nylo provides a .env
file which contains global configuration variables like the app name, default locale and your App's environment.
This file is located at the root of your project, named ".env".
APP_NAME=Nylo
APP_ENV=local
APP_DEBUG=true
APP_URL=https://nylo.dev
ASSET_PATH_PUBLIC=public/assets/
ASSET_PATH_IMAGES=public/assets/images
TIMEZONE=UTC
DEFAULT_LOCALE=en
You can add new variables here and then fetch them using the getEnv()
helper.
Accessing values from the .env file
You can access your .env
values anywhere in the app using the below helper.
import 'package:nylo_framework/nylo_framework.dart';
String appName = getEnv('APP_NAME');
Environment Configuration
Configuring your applications environment is simple.
First open your .env
file and then update the keys in the environment file.
You can also add additional keys here, e.g. SHOW_ADS="false"
.
Your .env file
APP_NAME="My Super App"
Your Text widget
Text(
"Hello, my app's name is " + getEnv('APP_NAME'),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontWeight: FontWeight.bold),
)
Best practises:
-
Don't store anything sensitive or large.
-
Don't commit your
.env
file to a (public/private) repository.
Environment Variable Types
The values in your .env file are defined as String
's but Nylo will return them differently if they match a type e.g. Boolean
or null
.
.env file |
Return type |
---|---|
APP_NAME="MySuper App" | String |
DEBUG_MODE=true | Boolean |
URL_TERMS=null | null |
Retrieving Environment Values
Fetching values from your .env
file is simple in Nylo, you can call the getEnv(String key)
helper.
String appName = getEnv('APP_NAME');
You can also provide a defaultValue if the key doesn't exist in the .env file.
String locale = getEnv('DEFAULT_LOCALE', defaultValue: "en");
int httpConnectionTimeout = getEnv('HTTP_CONNECTION_TIMEOUT', defaultValue: (60 * 1000));
Environment flavours
In Nylo, you can build your application from different environment 'flavours'. This allows you to create separate .env
files e.g. 'production', 'staging' or 'developing' and then build your app from the configuration.
Creating an environment flavour
First, make a new file at the root level of your project, e.g. .env.production
. Then, you can define all your env variables in this file.
Next, add the new env file to your 'assets' in the pubspec.yaml
file.
pubspec.yaml
assets:
...
- .env
- .env.production // -- new
Then, from the terminal, build your Flutter app by specifying the env file like in the below example.
flutter run --dart-define="ENV_FILE=.env.production"
flutter run --dart-define="ENV_FILE=.env.developing"
// or build
flutter build ios --dart-define="ENV_FILE=.env.developing"
flutter build appbundle --dart-define="ENV_FILE=.env.developing"
The
ENV_FILE
will default to.env
.