问题
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