Envied : Simplifying Flutter’s Environment Variables



Envied is a Flutter package that simplifies the management of environment variables in your application. It provides an easy way to store and access sensitive information. With Envied, you can securely define environment variables in a .env file and effortlessly retrieve them throughout your Flutter project. It enhances the security and organization of your app by streamlining the management of sensitive information.

Step 1: Adding Envied and Build Runner Dependencies

To get started, we need to add the Envied and build_runner packages as dependencies to our Flutter project. Open your pubspec.yaml file and add the following lines under the dependencies and dev_dependencies sections:

envied: ^1.0.0

build_runner: ^2.0.0

Step 2: Creating a .env File:

Next, let’s create a .env file in the root directory of your Flutter project. This file will store your environment variables securely. Open the .env file and define your key-value pairs in the following format:

PASSWORD_KEY = "admin123"

Feel free to add additional variables as needed for your project.

Step 3: Creating Env Class

To generate the necessary code to access the environment variables defined in the .env file, we need to create an env.dart file. Create a new Dart file, such as env.dart, and add the following code:

import 'package:envied/envied.dart';
part 'env.g.dart';

abstract class Env {

@EnviedField(varName: 'API_KEY', defaultValue: '')
static String apiKey = _Env.apiKey;

@EnviedField(varName: 'BASE_URL', defaultValue: '')
static String baseUrl = _Env.baseUrl;

@EnviedField(varName: 'PASSWORD_KEY', defaultValue: '', obfuscate: true)
static String passwordKey = _Env.passwordKey;


In the code, the Env class is declared as an abstract class and annotated with @Envied(). This annotation indicates that the class should be processed by the Envied package.

Inside the Env class, environment variables are defined as static variables using the @EnviedField annotation. Each variable is associated with a specific variable name from the .env file. A defaultValue can be provided to handle cases where the variable is not found in the .env file.

Additionally, the obfuscate option can be used with the @EnviedField annotation to obfuscate sensitive values when generated. This adds an extra layer of security to protect sensitive information in the code.

Step 4: Generating Code with Build Runner:

To generate the required code for accessing the environment variables, we’ll use the build_runner package. Open a terminal, navigate to your Flutter project directory, and run the following command:

flutter pub run build_runner build

This command triggers the code generation process and creates the env.g.dart file, which contains the necessary code to access the environment variables.

Step 6: Accessing Environment Variables in Flutter

Once you have set up the Envied package and generated the code, accessing your environment variables is easy. Simply import the env.dart file in your Dart file, where you want to use the variables, and now you can access the environment variables using static getters from the Env class:

import 'env.dart';

String apiKey = Env.apiKey;
String baseUrl = Env.baseUrl;
String passwordKey = Env.passwordKey;

By following these steps, you can effortlessly access your environment variables in your Flutter app.

(TIP): Using environment variables with pipelines:

When using pipelines or CI/CD workflows, it’s essential to provide environment variables to your build process securely. Here are some steps to consider:

- Consult your pipeline or CI/CD system’s documentation to learn how to set and manage environment variables.

- Add the required environment variables, such as the API key and base URL, to your pipeline or CI/CD system’s environment variable configuration.

- Ensure that the environment variables are securely passed to your build process and not exposed in plain text. Refer to your pipeline system’s documentation for best practices on handling sensitive information.

If you found this article helpful, feel free to follow me on LinkedIn for more insightful content. Also, don’t forget to check out my GitHub profile for more exciting projects and code samples. Happy coding!

LinkedIn: https://www.linkedin.com/in/bilalrehman08/

Github : https://github.com/BilalRehman08



Muhammad Bilal Rehman Khan

Senior Flutter Developer | Flutter Trainer | Software Engineer | Mobile App Developer