Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart web app environmental variables

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.)

like image 280
IAmYourFaja Avatar asked Mar 22 '23 04:03

IAmYourFaja


2 Answers

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

like image 86
Matt B Avatar answered Mar 23 '23 18:03

Matt B


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'];
}
like image 26
Alexandre Ardhuin Avatar answered Mar 23 '23 18:03

Alexandre Ardhuin