I am configuring an API endpoint using the flutter package dio
! I have two API endpoints: one for development and another for production. I want my dio
baseURL to automatically switch between development and production endpoints.
Just like so
Dio(
baseURL: isProduction ? productionBaseURL : developmentBaseURL
......
);
How to know if my app is in production in Dart?
CodePudding user response:
Just check the kReleaseMode
global constant. It's true
if the application was compiled in release mode.
Or, I'd recommend that this info comes from an environment variable so it follows one of the bullets from The Twelve-Factor App (the Config bullet). And to get it and check do the following, for example, IS_PRODUCTION
environment variable:
final isProduction = String.fromEnvironment('IS_PRODUCTION') == '1';
CodePudding user response:
You have to decide and check yourself what "production" means in a way that suits your app. It can be a runtime setting for example in in shared_preferences, or it can be a compile-time setting you pass to flutter build
.
I have implemented a pub.dev package firebase_options_selector that does this at runtime with a Firebase backend. You can use my source code as inspiration if you want.
CodePudding user response:
Use can use flavors in flutter to setup two different environments for your app, one for testing and one for production. You can refer this official documentation.