author avatar

sujay

Thu Dec 14 2023

Use environmental variables in flutter

  • Install flutter_dotenv package
  • Create a .env file in the root of your project
  • Add your variables to the .env file
  • In order for the package to load .env file, add it to asset bundle in pubspec.yaml
  flutter:
    assets:
      - .env (path to the file where env is stored)
  • Load the contents of env into flutter, by adding this is main.dart
 void main() async {
    await dotenv.load();
    runApp(const MyApp());
  }
  • Access the env variable like this dotenv.env['APP_ENVIRONMENT']