Have you ever faced an issue where you have to create multiple builds for different flavors? Isn't it tiring and irritating? What if you could use the same build for different flavors?
In this article, we will see how we can use Remote Config to use the same build for multiple flavors!
How to use Firebase Remote Config
So to use Remote Config, first go to the Firebase Console and from the left panel, go to Remote Config.
When you click on Create Configuration, you will be able to add a new key-value pair. Let's add key as flavor and value as dev. And then you will have to publish those changes.
Now, let's implement Remote Config in our application. For this, you will have to add firebase_remote_config package in your pubspec.yaml file along with firebase_core package. You will also have to add firebase_analytics because it is now mandatory to use it along with remote config.
The next step is to create a service file that will contain all the remote config code.
In the above code snippet, we have first created the instance of the remote config. Using that particular instance, we set the configuration settings where specifically we set fetchTimeout and minimumFetchInterval
fetchTimeout: Maximum Duration to wait for a response when fetching configuration from the Remote Config server. Defaults to one minute.
minimumFetchInterval: Maximum age of a cached config before it is considered stale. Defaults to twelve hours. Hence, to get the updated new value quickly, we are setting the minimumFetchInterval to 1 second!
Once we set the configurations, the next step is to fetch and activate the remote config data. After this step, you can easily get the value of a particular key using getString method. There are more methods based on different data types of the value and even a generic method called getValue(). If you want to fetch all the values, we also have thegetAll() method.
Now, based on the flavor value received, you can easily set the API Endpoints that you want 🥳
Hence, this is how you can easily use the same build for multiple flavors. Remote Config has its other use-cases too, for example, providing dynamic updates to your app, dynamically changing the theme, font, etc.
Feel free to check the complete example from our GitHub Repository!