Environment variables in javascript using Dart2JS

Environment variables are cool. On the server side you can set some environment variables which will let you switch some functionality or value when deploying to production.

But what about front-end apps? What's the best way to have a development and production "environment" so that when in development you are pointing to your development endpoint, and when you build your dart app for production (using pub build), you automatically switch to your production endpoint, in Javascript?

The answer is ".fromEnvironment". For String and bool datatype, Dart gives you a constant constructor called fromEnviorment that reads the environment variable and sets it to your constant.

For example

bool isRelease = const bool.fromEnvironment("releasemode");

But how do you populate "releasemode"?

When you run pub build, simply pass in the environment variable as such

pub build --define releasemode=true

and voila! you have environment variables on the front end!

Now that is pretty cool.

The possibilities are endless, personally I use it to switch from a stage endpoint to a production endpoint when I deploy, but you can use it for anything!