Flutter. Using environment variables.

Image for post
Image for post

Step 1

First we’ll need to add flutter_dotenv package to our dependencies:

flutter_dotenv: ^2.1.0

Step 2

Import flutter_dotenv package in main.dart file:

import 'package:flutter_dotenv/flutter_dotenv.dart';

Step 3

Next, we will change our main method

void main() => runApp(MyApp());
Future main() async {
await DotEnv().load('.env');
runApp(MyApp());
}

Step 4

To extract .env variable, VAR_NAME=HELLO_WORLD, for example, we will need to use this syntax:

import 'package:flutter_dotenv/flutter_dotenv.dart';// some classprint(DotEnv().env['VAR_NAME'])
Image for post
Image for post

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store