Is there a nice way to have different configuratio

2019-07-21 07:29发布

问题:

I've created an app that requires some configuration. The way I'm doing it now is a bit hacky and tedious -- If I'm developing locally, I include the "local" configuration file (a dart file that defines a few consts) and if deploying, I include a different dart file that defines the same consts with different values.

Ideally, there would be a way to define different configurations, which could be passed to pub serve/build. It seems like such an obvious need that I feel like something may already exist and I just haven't run across it yet. Is there? Or is something in the works?

回答1:

Pub build does not support defining environment variables but you can use fromEnvironment method, for example, on String:

String.fromEnvironment(String name, {String defaultValue})

Returns the string value of the environment declaration name. Environment declarations are provided by the surrounding system compiling or running the Dart program. Declarations map a string key to a string value.

Or make it "fancier" with transformers. It will allow you to automatically(on pub build or pub serve) get settings from pubspec.yaml or another source and embed them in to the code. For example sass(it is transformer) package supports settings of this form:

:::yaml
transformers:
  - sass:
      executable: /path/to/sass     # Sass executable to use
      compass: true                 # Include compass
      line-numbers: true            # Include line numbers in output
      style: compact                # Style of generated CSS
      copy-sources: true            # Copy original .scss/.sass files to output directory

With it you can do pretty much anything including source code modification.



标签: dart