env variables flutter code example

Example 1: use env flutter

You can do that by inserting in the .env file:

HOST=localhost
PORT=3000

Add the .env file in the assets section of the pubspec.yaml:

assets:
  - .env

Then, you can change the main function in the main.dart to load the .env file:

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

After that, you can get the HOST and PORT anywhere with:

DotEnv().env['PORT'];
DotEnv().env['HOST'];

All these instructions are in the README of the library: https://pub.dev/packages/flutter_dotenv#-readme-tab-

Example 2: flutter environment variables

const bool isProduction = bool.fromEnvironment('dart.vm.product');