Dart web app environmental variables

733 views Asked by At

It would be nice to allow my Dart web app to hit different servers depending on what environment it was deployed on:

  • DEV: http://dev.myapp.com/someService
  • QA: http://testing.myapp.com/someService
  • LIVE: http://myapp.com/someService

In Java, typically you'd have a deployment descriptor (myapp.properties) that the app reads off the runtime classpath, allowing you to specify a myapp.properties on DEV like so:

service.url=dev.myapp.com/someService

And on QA like so:

service.url=qa.myapp/com/someService

etc. It looks like Dart offers something comparable however its server-side/command-line only...

So how do Dart web developers achieve the same thing, where you don't need to hardcode all of your various environments' servers into the app? (Obviously, this question extends beyond service URLs and really applies to any environment-specific property.)

2

There are 2 answers

3
Matt B On BEST ANSWER

You can use the String.fromEnvironment constant constructors to get values passed to the dart2js compilers. For a full explaination on this new functionality check out Seth Ladd's blog post: Compile-time dead code elimination with dart2js

0
Alexandre Ardhuin On

To keep the same build you can read a variable from html which could be generated on server side.

For instance the server could generate (or replace with templating) the html file with :

<script>
  // var serviceUrl = "@VALUE_OF_VAR@";
  var serviceUrl = "dev.myapp.com/someService";
</script>

and in dart client file :

import 'dart:js' as js;
main() {
  var serviceUrl = js.context['serviceUrl'];
}