Dart 2 AngularDart how to config env vars in deployment environment?

Issue

I need to use different config values when deploying my application vs when in development. How to set this up with Dart 2 using webdev serve and webdev build ?

Solution

The build.yaml allow you to add config variable that are different in development than in production.

Here is an example of a build.yaml

targets:
  $default:
    sources:
      include: ["lib/**", "web/**"]
    builders:
      build_web_compilers|entrypoint:
        release_options:
         dart2js_args:
           - -Dhost=https://example.com
           - -DenableFeatureOne=true

Then you can get the value of host and enableFeatureOne by using:

final host = const String.fromEnvironment('host', defaultValue: 'http://localhost:8080');

So in development the host will be http://localhost:8080 and in production after compiling with dart2js it will be https://example.com.
Same for enableFeatureOne.

release_options will be only used when compiled with the build command or with pub run build_runner build -o build --release

Development config should be the default one since there is no way to pass different config value to ddc. A workaround is to make a GET request on a file with this value when your app start.

Answered By – Kleak

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

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