Nylo provides a .env file which contains global configuration variables like the App Name, default locale and private API keys.

You can access these values anywhere in the app using the below helper.

import 'package:nylo_support/helpers/helper.dart';

String appName = getEnv('APP_NAME');

The .env file can be found at the root of the project and should look similar to the below.

You can add new variables here such as strings and booleans.



Environment configuration

Environment variables are great for using anywhere in our applications. A quick example would be if we wanted to set the name of our application to e.g. MySuper App and then use the same value anywhere in our widgets.

Your .env file

APP_NAME="My Super App"

Your Text widget

  "Hello, my app's name is " + getEnv('APP_NAME'),
  overflow: TextOverflow.ellipsis,
  style: TextStyle(fontWeight: FontWeight.bold),

To add global configuration variables to your project, you can use the .env file to store add any sensitive values too. This file is useful for adding API keys and any information you consider sensitive. It could also be used for anything you might need to access globally throughout your app.

{danger} It's important to note that this file should never be committed to your repository (if using version control). By default, it's included within your .gitignore file.

Environment Variable Types

The .env values are set as type string when you are defining them but Nylo will return some values types in a more helpful way like booleans and null values.

.env file .env return type
DEBUG_MODE=true (bool) true
SHOW_ADS=false (bool) false
APP_NAME="MySuper App" (string) "My Super App"
MAPS_API_KEY=null (null) null

Retrieving environment values

Fetching values from your .env file is simple in Nylo, you can call the getEnv(String key) function.

String appName = getEnv('APP_NAME');

You can also provide a defaultValue if the key doesn't exists in the .env file.

String locale = getEnv('DEFAULT_LOCALE', defaultValue: "en");

int httpConnectionTimeout = getEnv('HTTP_CONNECTION_TIMEOUT', defaultValue: (60 * 1000));