Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a generated variable based on flavor #31441

Closed
guitcastro opened this issue Apr 22, 2019 · 11 comments
Closed

Create a generated variable based on flavor #31441

guitcastro opened this issue Apr 22, 2019 · 11 comments
Labels
c: new feature Nothing broken; request for a new capability tool Affects the "flutter" command-line tool. See also t: labels.

Comments

@guitcastro
Copy link

As today there is no hint to get what flavor is running in dart code. The current workaround is to use the -t flag in the build command to change the main file target, creating main_dev.dart and main_prod.dart per example.

Would be nice to have some generated variable or something similar to expose which flavor is in compile time.

@BondarenkoStas BondarenkoStas added c: new feature Nothing broken; request for a new capability tool Affects the "flutter" command-line tool. See also t: labels. labels Oct 12, 2019
@jmagman jmagman added this to Awaiting triage in Tools - team support review via automation Jan 15, 2020
@zanderso
Copy link
Member

This sounds like something that could be plumbed to String.fromEnvironment(). /cc @jonahwilliams

@zanderso zanderso moved this from Awaiting triage to Engineer reviewed in Tools - team support review Jan 23, 2020
@jonahwilliams
Copy link
Member

We do have the --dart-defines option, but that isn't wired up for non-JavaScript targets yet. We could do something automatically, but for the most part the dart code doesn't depend on chosen flavor, so we'd introduce another angle of invalidation. This might waste some time if the flavor-dependent environment variable would otherwise not be used

@zanderso
Copy link
Member

Is there an issue filed to wire up --dart-defines for non-Web platforms?

@jonahwilliams
Copy link
Member

#44483

@friebetill
Copy link

Now that #44483 is solved, what would happen next?

@jonahwilliams
Copy link
Member

In your code:

const String flavor = String.fromEnvironment('app.flavor');

void main() {
  print(flavor);
}

From command line:

flutter build ios --flavor=paid --dart-define=app.flavor=paid

This would print "paid" when run.

@friebetill
Copy link

That's wonderful. Are there any plans to avoid having to double-specify the flavor?

@jonahwilliams
Copy link
Member

doing this automatically would require us to fully invalidate a build whenever someone switched flavors. That might be surprising if someone wasn't using the actual constant value

@jonahwilliams
Copy link
Member

I think this can be scripted easily enough from the command line. closing as resolved

@hicnar
Copy link

hicnar commented Apr 8, 2021

@jonahwilliams Your proposed solution does not work if one tries to run an app on iOS (ex flutter run --flavor=paid --dart-define=app.flavor=paid) neither on simulator nor on a real device.

For some reason no dart defines are passed into the environment so that they could be subsequently read by String.fromEnvironment(...).

It does seem to work on Android though, but with Android the main method can be defined in an arbitrarily named dart file and again as opposed to iOS the -t ... / --target=... arguments work perfectly well.

I think observations by Denis Baketsky may be relevant here: https://medium.com/@tatsu.ukraine/what-you-should-know-before-you-upgrade-flutter-in-your-project-with-compile-time-variables-3ec3d2e9ba79

In your code:

const String flavor = String.fromEnvironment('app.flavor');

void main() {
  print(flavor);
}

From command line:

flutter build ios --flavor=paid --dart-define=app.flavor=paid

This would print "paid" when run.

@github-actions
Copy link

github-actions bot commented Aug 3, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: new feature Nothing broken; request for a new capability tool Affects the "flutter" command-line tool. See also t: labels.
Projects
Tools - team support review
  
Engineer reviewed
Development

No branches or pull requests

6 participants