7:25 PM
-1
Q: Does Flutter have RUNTIME environment variables?

JoshFrom what I can tell, Flutter doesn't have the ability to set true runtime environment variables. What Flutter calls environment variables seem to be nothing more than build arguments. I would be happy to be proven wrong on this. For example, you can pass in environment variables (build args) li...

 
@halfer Sure. Here's a line in a file called env.dart: const myEnvVar = String.fromEnvironment('MY_ENV_VAR', defaultValue: 'foobar');
 
Rephrased: how do I fetch/use runtime app config instead of environment variables in flutter
 
@AdamJenkins If that would help me achieve my goal, then sure.
 
you just want config, right? Fetch it from a server (or read it from one of multiple config files that are bundled in your app) and stick it in a class and have your app use it.
 
@AdamJenkins If I understand you correctly, yes I just want config, but I want different configs for the same docker image. So at runtime I can have docker run -p 80:80 -e MY_ENV_VAR=prodValue my-flutter-web-app for a prod deployment, and docker run -p 80:80 -e MY_ENV_VAR=devValue my-flutter-web-app for a dev deployment.
 
7:25 PM
Put both sets of configs in config files - like config.dev.yaml and config.prod.yaml - and rename the config you want to use (as specified by your env variable) to config.yaml before you start your app, and have your flutter app always look for config in config.yaml. Runtime config
 
@AdamJenkins Could you elaborate on this approach? We can take it to a private chat if needed.
 
There’s several ways to do it, the general premise is to get your configuration from a file at runtime, as opposed to using build time environment variables
I’ve done this before (not with a flutter app) by using an entry point script that reads env vars, uses jq to put them in a config.json file, and then starts my app. My app just reads config.json at runtime and has that configuration
So, while I don’t have a specific code sample for you, I think understanding the premise will help you find the code you need. Don’t use build time env variables, use a config.json or a config.yaml file that can be read by the run time and then focus your efforts on getting environment variables into that config file
 
Ok. so I'm doing something a bit similar right now, but it's too hacky. I have an entrypoint script that has this: flutter build web --release --dart-define ENV_VAR1=${ENV_VAR1:-} --dart-define ENV_VAR2=${ENV_VAR2:-}.

So the build happens at runtime. I don't like this approach.
 
What’s hacky about it? Why don’t you like it?
 
In response to your last comment, I don't think flutter can read from a config.json at runtime, and that's the issue.
 
7:32 PM
Flutter can absolutely read from the file system
 
Well it's hacky because the build should happen during the docker build stage imo, not during docker run. It makes for a slow loading also when starting the container.
Ok, I probably shoudl look into just reading from the file system then instead of the standard flutter environment variable methodologies.
 
Ok, good point, that’s why you don’t want to build, because you’re still using build time variables. Which you don’t want. You want runtime configuration.
your app should start, and the first thing it does is check for the config file in the file system and read it (and validate it)
Like I said, now you’ve moved your problem space to “how do I get these values into a config file during docker run” and “how do I read from the file system in flutter”
Firebase does this with a thing called “remote config” which the app just fetches on start. It’s the same concept - the app is built already (and shipped) but the values it needs need to come from “somewhere else”
 
ok
that's good to know
I'll look into this further. I don't want to take up all your time. Thanks Adam!
 
👍