Configuration



Introduction

Most apps will need a simple way to manage global configuration variables like the App Name, default locale and private API keys. Nylo provides a simple way to handle this in your Flutter projects, continue reading to learn more.


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

Text(
  "Hello, my app's name is " + getEnv('APP_NAME'),
  textAlign: TextAlign.center,
  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.

Where is the .env file?

The location of the .env file is at the root of the project, by default it won't exist but we include a .env-example that you can use to create the .env file.

{danger} It's important to note that this file should never be committed to your repository (if using source 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() function.

String appName = getEnv('APP_NAME');