Flutter run
and build
commands accept the --flavor
parameter. I have found a tutorial on implementing it in Android and iOS projects. And, I also found a way to access the flavor name from the flutter code.
But I still have no idea how to access the flavor name on other platforms. It seems flutter documentation has no information about it.
Is there a way to read the --flavor
parameter value from the flutter code on any platform?
Asked
Active
Viewed 537 times
0

hurelhuyag
- 1,691
- 1
- 15
- 20
-
Just for clarity... What do you want to achieve? Pass in an argument (lets call it flavor) upon build that can be accessed in the flutter code? – Robert Sandberg Dec 10 '22 at 07:38
-
Afaik the flavor parameter is intended to be used for the building of the app. I use it for that. I also pass an environment variable to the app itself to tell it that it is a dev or prod app. That environment variable should work on any platform. – GrahamD Dec 10 '22 at 07:49
-
@RobertSandberg I want a single parameter to control environment variables. Every other tutorial depends on 2 or more parameters. – hurelhuyag Dec 10 '22 at 08:00
1 Answers
0
The reason for my question in the comments is that you might be looking for using the --dart-define
flag when building your app, instead of --flavor
.
They serve different purposes, so both might be required depending on your desired outcome.
The value passed via the dart define flag can be accessed in the code via String.fromEnvironment(...)

Robert Sandberg
- 6,832
- 2
- 12
- 30
-
It seems I still need 2 parameters. `--dart-define` to define variables. And, `--flavor` to change the app name and icon for iOS and Android. – hurelhuyag Dec 10 '22 at 08:05
-
Yeah, I think it is needed. Unless something has changed that I'm not aware of. Is there a reason why you can only use one? – Robert Sandberg Dec 10 '22 at 08:07
-
It makes possible that someone can create an invalid build. For example `--dart-define="env=prod" --flavor=staging` – hurelhuyag Dec 10 '22 at 08:09
-
It makes sense to have two parameters for dev and prod for this reason: The flavor determines the type of build - debug or release. The former build will have debug tools included in the app and it is therefore larger and less performant (eg. for cpu testing). The dev or prod environment variable tells the app eg. to point at a dev or a prod database. So, you may well want to run debug/dev, debug/prod (unlikely), release/dev, release/prod versions. – GrahamD Dec 10 '22 at 08:11
-
I agree with GrahamD. There are many ways to configure an invalid build. One is to write a flavor that isn't handled (`flavor=staydging"`). At some point you'd have to make sure to make the correct things to make a build valid. – Robert Sandberg Dec 10 '22 at 08:15
-