|
| 1 | +--- |
| 2 | +title: Get started with Firebase Remote Config |
| 3 | +sidebar_label: Get started |
| 4 | +--- |
| 5 | + |
| 6 | +You can use Firebase Remote Config to define parameters in your app and update |
| 7 | +their values in the cloud, allowing you to modify the appearance and behavior of |
| 8 | +your app without distributing an app update. |
| 9 | +This guide walks you through the steps to get started and provides some |
| 10 | +sample code. |
| 11 | + |
| 12 | +## 1. Add Firebase and the Remote Config SDK to your app |
| 13 | + |
| 14 | +1. [Install and initialize the Firebase SDKs for Flutter](../overview) if you |
| 15 | + haven't already done so. |
| 16 | + |
| 17 | +1. For Remote Config, Google Analytics is required for the |
| 18 | + [conditional targeting of app instances](https://firebase.google.com/docs/remote-config/parameters#conditions_rules_and_conditional_values) |
| 19 | + to user properties and audiences. Make sure that |
| 20 | + you <a href="https://support.google.com/firebase/answer/9289399#linkga" |
| 21 | + class="external">enable Google Analytics</a> in your project. |
| 22 | + |
| 23 | +1. From the root directory of your Flutter project, run the following |
| 24 | + command to install the Remote Config plugin: |
| 25 | + |
| 26 | + ```bash |
| 27 | + flutter pub add firebase_remote_config |
| 28 | + ``` |
| 29 | + |
| 30 | + Also, as part of setting up Remote Config, you need to add the Firebase SDK |
| 31 | + for Google Analytics to your app: |
| 32 | + |
| 33 | + ```bash |
| 34 | + flutter pub add firebase_analytics |
| 35 | + ``` |
| 36 | + |
| 37 | +1. Rebuild your project: |
| 38 | + |
| 39 | + ```bash |
| 40 | + flutter run |
| 41 | + ``` |
| 42 | + |
| 43 | +:::note |
| 44 | +Because the Remote Config SDK has a dependency on the Remote Config REST |
| 45 | +API, make sure that you do **not** disable that API, which is enabled by default |
| 46 | +in a typical project. |
| 47 | +::: |
| 48 | + |
| 49 | +## 2. Get the Remote Config singleton object |
| 50 | + |
| 51 | +Get a Remote Config object instance and set the |
| 52 | +minimum fetch interval to allow for frequent refreshes: |
| 53 | + |
| 54 | +```dart |
| 55 | +final remoteConfig = FirebaseRemoteConfig.instance; |
| 56 | +await remoteConfig.setConfigSettings(RemoteConfigSettings( |
| 57 | + fetchTimeout: const Duration(minutes: 1), |
| 58 | + minimumFetchInterval: const Duration(hours: 1), |
| 59 | +)); |
| 60 | +``` |
| 61 | + |
| 62 | +The singleton object is used to store in-app default parameter values, fetch |
| 63 | +updated parameter values from the backend, and control when fetched values are |
| 64 | +made available to your app. |
| 65 | + |
| 66 | +During development, it's recommended to set a relatively low minimum fetch |
| 67 | +interval. See [Throttling](#throttling) for more information. |
| 68 | + |
| 69 | +## 3. Set in-app default parameter values |
| 70 | + |
| 71 | +You can set in-app default parameter values in the Remote Config |
| 72 | +object, so that your app behaves as intended before it connects to the |
| 73 | +Remote Config backend, and so that default values are available if none are |
| 74 | +set in the backend. |
| 75 | + |
| 76 | +```dart |
| 77 | +await remoteConfig.setDefaults(const { |
| 78 | + "example_param_1": 42, |
| 79 | + "example_param_2": 3.14159, |
| 80 | + "example_param_3": true, |
| 81 | + "example_param_4": "Hello, world!", |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +## 4. Get parameter values to use in your app |
| 86 | + |
| 87 | +Now you can get parameter values from the Remote Config object. If you set |
| 88 | +values in the backend, fetch them, and then activate them, |
| 89 | +those values are available to your app. Otherwise, you get the in-app |
| 90 | +parameter values configured using `setDefaults()`. |
| 91 | + |
| 92 | +To get these values, call the method listed below that maps to the data type |
| 93 | +expected by your app, providing the parameter key as an argument: |
| 94 | + |
| 95 | +* `getBool()` |
| 96 | +* `getDouble()` |
| 97 | +* `getInt()` |
| 98 | +* `getString()` |
| 99 | + |
| 100 | +## 5. Set parameter values in the Remote Config backend |
| 101 | + |
| 102 | +Using the Firebase console or the |
| 103 | +[Remote Config backend APIs](/docs/remote-config/automate-rc), |
| 104 | +you can create new server-side default values that override the in-app values |
| 105 | +according to your desired conditional logic or user targeting. This section |
| 106 | +describes the Firebase console steps to create these values. |
| 107 | + |
| 108 | +1. In the [Firebase console](https://console.firebase.google.com/), open your project. |
| 109 | +1. Select **Remote Config** from the menu to view the Remote Config |
| 110 | + dashboard. |
| 111 | +1. Define parameters with the same names as the parameters that you defined in |
| 112 | + your app. For each parameter, you can set a default value (which will |
| 113 | + eventually override the corresponding in-app default value), and you can also |
| 114 | + set conditional values. To learn more, see [Remote Config Parameters and |
| 115 | + Conditions](https://firebase.google.com/docs/remote-config/parameters). |
| 116 | + |
| 117 | +## 6. Fetch and activate values |
| 118 | + |
| 119 | +1. To fetch parameter values from the Remote Config backend, call the |
| 120 | + `fetch()` method. Any values that you set in the backend are fetched |
| 121 | + and stored in the Remote Config object. |
| 122 | + |
| 123 | +1. To make fetched parameter values available to your app, call the |
| 124 | + `activate()` method. |
| 125 | + |
| 126 | + For cases where you want to fetch and activate values in one call, you |
| 127 | + can use a `fetchAndActivate()` request to fetch values from the |
| 128 | + Remote Config backend and make them available to the app: |
| 129 | + |
| 130 | + ```dart |
| 131 | + await remoteConfig.fetchAndActivate(); |
| 132 | + ``` |
| 133 | + |
| 134 | +Because these updated parameter values affect the behavior and appearance |
| 135 | +of your app, you should activate the fetched values at a time that ensures a |
| 136 | +smooth experience for your user, such as the next time that the user opens your |
| 137 | +app. See [Remote Config loading strategies](https://firebase.google.com/docs/remote-config/loading) |
| 138 | +for more information and examples. |
| 139 | + |
| 140 | +## Throttling {#throttling} |
| 141 | + |
| 142 | +If an app fetches too many times in a short time period, fetch calls will be |
| 143 | +throttled and the value of `FirebaseRemoteConfig`'s `lastFetchStatus` |
| 144 | +property will be `RemoteConfigFetchStatus.throttle`. |
| 145 | + |
| 146 | +The default minimum fetch interval for Remote Config is 12 hours, which |
| 147 | +means that configs won't be fetched from the backend more than once in a 12 hour |
| 148 | +window, regardless of how many fetch calls are actually made. |
| 149 | + |
| 150 | +During app development, you might want to fetch and activate configs very frequently |
| 151 | +(many times per hour) to let you rapidly iterate as you develop and test your |
| 152 | +app. To accommodate rapid iteration on a project with up to 10 developers, you |
| 153 | +can temporarily set a low minimum fetch interval with `setConfigSettings()`. |
| 154 | + |
| 155 | +```dart |
| 156 | +final remoteConfig = FirebaseRemoteConfig.instance; |
| 157 | +await remoteConfig.setConfigSettings(RemoteConfigSettings( |
| 158 | + fetchTimeout: const Duration(minutes: 1), |
| 159 | + minimumFetchInterval: const Duration(minutes: 5), |
| 160 | +)); |
| 161 | +``` |
| 162 | + |
| 163 | +:::caution |
| 164 | +Keep in mind that this setting should be used for development only, not for an |
| 165 | +app running in production. If you're just testing your app with a small |
| 166 | +10-person development team, you are unlikely to hit the hourly service-side |
| 167 | +quota limits. But if you pushed your app out to thousands of test users with a |
| 168 | +very low minimum fetch interval, your app would probably hit this quota. |
| 169 | +::: |
| 170 | + |
| 171 | +## Next steps |
| 172 | + |
| 173 | +If you haven't already, explore the Remote Config |
| 174 | +[use cases](https://firebase.google.com/docs/remote-config/use-cases), and take a look at some of the |
| 175 | +key concepts and advanced strategies documentation, including: |
| 176 | + |
| 177 | +* [Templates and versioning](https://firebase.google.com/docs/remote-config/templates) |
| 178 | +* [Config loading strategies](https://firebase.google.com/docs/remote-config/loading) |
0 commit comments