DART: What is best practice for config settings

Issue

Interpreted languages such as PHP allow a separate file, often called config.php, to contain string constants such as server names. This facilitates deployment, as the config file is simply not uploaded when the code is updated – the server names, e.g. for REST transactions, are typically different in the deployment environment.

In Dart, since it is compiled, this approach does not work. If there are server name constants which are referred to in the HTML via {{ }}, it seems the code must be recompiled before deployment.

Is there a way to specify string constants in such a way to avoid this recompilation requirement?

Solution

There are a couple of options I can think of:

One trick is to put configuration in a map keyed by hostname. At runtime, look up the configuration from the map, using window.location as a key. This will allow configuration to be baked into the Dart source, yet still allow different values to be specified for different environments.

If you want to be able to change your configuration after compilation, you can embed it as JSON within the HTML source, or load it via an HTTP request. (This isn’t using a constant as asked, however, by definition it’s not possible to change a constant after compile time)

Answered By – Greg Lowe

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *