Skip to content

osama96gh/flutter-env-secrets

Repository files navigation

Flutter Env Secrets Example

An example Flutter project demonstrating how to securely pass environment variables using --dart-define-from-file.

Setup

  1. Ensure Flutter is Updated

    Upgrade Flutter if needed:

    flutter upgrade
  2. Create Environment Files

    • Development (env.dev.json) and Production (env.prod.json) Files:

      {
        "API_URL": "https://your-api.example.com",
        "API_KEY": "your_api_key"
      }

    Note: Replace placeholder values with your actual API URLs and keys.

  3. Add Environment Files to .gitignore

    env.dev.json
    env.prod.json
  4. Update lib/main.dart

    Use String.fromEnvironment to access the variables:

    static const apiUrl = String.fromEnvironment('API_URL', defaultValue: 'Not Found');
    static const apiKey = String.fromEnvironment('API_KEY', defaultValue: 'Not Found');

Simple Usage

  • Run the App in Development Mode:

    flutter run --dart-define-from-file=env.dev.json
  • Build the App for Production:

    flutter build apk --release --dart-define-from-file=env.prod.json

CI/CD Integration

  • Store environment variables securely in your CI/CD platform and generate environment files at build time.

Feel free to reach out if you have any questions!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published