Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

environment variables for firebase when hosting flutter

I am hosting flutter web app on firebase. How do I pass env (non secret) variables to firebase? I deploy with firebase deploy command.

When running flutter locally, I pass env variable as a flag in command line argument:

flutter run --dart-define=envFileName=local.env --web-browser-flag "--disable-web-security"

like image 496
Aseem Avatar asked Jun 26 '26 18:06

Aseem


1 Answers

I just spent all day figuring this out. I was trying to get Github actions working. So the official docs say to use 'source' in your firebase.json file. https://firebase.google.com/docs/hosting/frameworks/flutter What isn't clear is that when you have source in the config, it will build the flutter app when you run firebase deploy. That clears out any build parameters you sent in previously, even if you just built with --dart-define parameters.

The solution is to change your firebase.conf file to something like this:

{
  "hosting": {
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "frameworksBackend": {
      "region": "us-central1"
    },
    "rewrites": [ {
   "source": "**",
   "destination": "/index.html"
} ]
  }
}

Then build your app

flutter build --dart-define=envFileName=local.env

Then deploy

firebase deploy

like image 146
Dan Somrack Avatar answered Jun 28 '26 08:06

Dan Somrack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!