Pass custom parameters to a dart application when using pub serve to run it

為{幸葍}努か 提交于 2019-12-11 08:45:27

问题


Is it possible to pass some argument to a dart application when running it using pub serve? What I'm trying to do is have an application use some mocked services while I'm developing it, but then when it's deployed I'd like to replace mocked services with real ones. For example:

const bool DEBUG = true;

class AppModule extends Module {
  AppModule() {
    type(PaymentService, implementedBy: DEBUG ? PaypalPaymentService : MockPaymentService );
  }
}

I'd like this DEBUG parameter to somehow come form the environment and to be easily configurable when running the application using pub serve. Which is the best way to achieve this?


回答1:


You could check the URL. If host is 127.0.0.1 your in the development environment, otherwise it's prod.

Another idea is to use a transformer that injects something when mode is debug. Not sure if that is really possible this way.

$ pub help serve
Run a local web development server.

Usage: pub serve
-h, --help               Print usage information for this command.
    --port               The port to listen on.
                         (defaults to "8080")

    --[no-]dart2js       Compile Dart to JavaScript.
                         (defaults to on)

    --[no-]force-poll    Force the use of a polling filesystem watcher.
    --mode               Mode to run transformers in.
                         (defaults to "debug")


来源:https://stackoverflow.com/questions/21210986/pass-custom-parameters-to-a-dart-application-when-using-pub-serve-to-run-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!