DEV Community

01kg
01kg

Posted on

1

Flutter Web | Build with .env File

Managing .env like files is a means of preventing hard-coded credentials and separating environments.

It is good to implement multiple .env files, e.g. .env.production, .env.development in Flutter app.

Then add some code before dotenv.load can do environments separation:

// main.dart
...
void main() async {
  const environment =
      String.fromEnvironment('ENV', defaultValue: 'development');
  await dotenv.load(fileName: '.env.$environment');
...
Enter fullscreen mode Exit fullscreen mode

Then run command like flutter build web --dart-define ENV=production to spin it up.

But, here is a pitfall. In my practical learning, a file starts with dot . could cause 404 issue:

404 error on fetching .env.production file

So, according to this suggestion, we'd better use "dot" instead of ".".

1. Rename .env.production, .env.development files to dotenv.production, dotenv.development

2. Update assets settings

Flutter's assets setting section

3. Update dotenv.load logic

// main.dart
...
void main() async {
  const environment =
      String.fromEnvironment('ENV', defaultValue: 'development');
  await dotenv.load(fileName: 'dotenv.$environment');
...
Enter fullscreen mode Exit fullscreen mode

4. Update .gitignore file

5. After building, delete unnecessary dotenv.* files in build > web > assets

files in build folder

In this case, dotenv.development is not necessary, so delete it to prevent potential sensitive data leaking.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of PulumiUP 2025

Transform Your Cloud Infrastructure

Join PulumiUP 2025 on May 6 for Expert Insights & Demos.

Register Now

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay