# `firebase_remote_config`

| Package | Kind | Provides the role | Needs | Choose with |
| --- | --- | --- | --- | --- |
| [`smf_firebase_remote_config`](https://pub.dev/packages/smf_firebase_remote_config) | infrastructure | Remote config | [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md) | `-m firebase_remote_config` |

`firebase_remote_config` reads the values of remote config of the app from [Firebase Remote Config](https://firebase.google.com/docs/remote-config), through the [firebase_remote_config](https://pub.dev/packages/firebase_remote_config) package. A value that you publish in the Firebase project changes the app without a new version of it, and each parameter has a default that the app reads while the project gives none.

The code of the app never uses the package. It reads the values through `appRemoteConfig`, which the remote config role generates; see [Remote config](https://doc.saymyframe.com/guides/remote-config.md). Firebase Remote Config works on the Firebase app, so the module depends on [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md), which comes with it and sets up Firebase for each flavor.

An app without parameters asks Firebase nothing. A parameter comes with a module that reads one, such as the lowest version of the app that may run of [`update_required`](https://doc.saymyframe.com/modules/update-required.md), or with one of your own.

## What it adds to the app

| In the app | What it is |
| --- | --- |
| `firebase_remote_config: ^6.7.1` in the dependencies | The package of Firebase Remote Config. 6.7 is the last version on firebase_core 4, which the Firebase modules share. |
| `lib/core/remote_config/firebase_remote_config_service.dart` | `FirebaseRemoteConfigService`, the remote config service of the app on `FirebaseRemoteConfig`, and `createFirebaseRemoteConfigService()`, which creates it. In `lib/`, only this file imports the package. |
| The section "Firebase Remote Config" of the README | Where the parameters go in the Firebase console, the two APIs of Google that the app needs, the fetch interval, what fetches cost, and what each fetch sends. |
| The step by hand `firebase_remote_config.parameters`, in an app with parameters | The parameters of the app in Remote Config; see [Finish the setup](#finish-the-setup). |

Whichever module provides it, the remote config role adds:

| File | What it holds |
| --- | --- |
| `lib/core/remote_config/app_remote_config.dart` | `appRemoteConfig`, through which the code of the app reads the values, and `initRemoteConfig()`. It exports `RemoteValues`, so the code of the app imports this file alone. |
| `lib/core/remote_config/remote_values.dart` | `RemoteValues`, with the default of each parameter of the app in `RemoteValues.defaults` and a getter for each. |
| `lib/core/remote_config/remote_config_service.dart` | `RemoteConfigService`, the interface that this module implements. |

The role also writes the section "Remote config" of the README, which lists the parameters of the app, each with its type, its default, its getter and the module that reads it.

In its platform phase, `bootstrap()` initializes Firebase and then starts remote config. This is `bootstrap()` of an app with this module alone:

```dart title="lib/bootstrap.dart"
Future<void> bootstrap() async {
  await Firebase.initializeApp(options: firebaseOptionsOfFlavor);
  await initRemoteConfig();
}
```

The service is created without waiting and asks Firebase nothing until `appRemoteConfig` opens it.

In the [guide for coding agents](https://doc.saymyframe.com/guides/coding-agents.md) of the app, the role describes `appRemoteConfig` under "Remote config". The module adds which file is on firebase_remote_config, where a parameter is created, which values of Firebase the service takes, why the app never activates values itself, and what a test that runs `bootstrap()` answers in place of the platform side of the package.

## When the values come

The values never hold the start of the app. `initRemoteConfig()` opens the service, which takes what the device kept, and makes active what the last fetch got, also without the network. It waits for that at most a second, and values that come later still become the values of the app.

In an app with parameters, the app then asks Firebase for new values in the background, and again each time the user comes back to the app. A release build asks at most every 12 hours, and a debug build at each start and each time you come back to the app, so a value that you change shows at once while you develop. The new values are active at once, also while a screen shows.

In such an app, `appRemoteConfig` also listens to the real-time updates of Firebase through the service: a value that you publish in the Firebase console reaches an app that runs, which makes it active at once, without waiting for the interval. An app that starts later gets it with its next fetch.

On Android, the real-time updates may stop until a later start of the app once it was in the background. Android may cut the network of an app soon after it leaves the screen, and Firebase then waits a minute or more before it connects again. If the user comes back to the app within that wait, Firebase for Android does not connect again while the app runs ([firebase/firebase-android-sdk#8827](https://github.com/firebase/firebase-android-sdk/issues/8827)). A value that you publish then reaches a release build at a later start, and a debug build the next time you come back to it, since the app does not fetch more often than its interval to make up for it.

| What the service does | Why |
| --- | --- |
| `open` takes what the device kept, then gives Firebase the timeout and the interval of a fetch of the role and the defaults of the parameters of the app. | The role sets how often the app asks, and the defaults are those of `RemoteValues.defaults`. |
| Its `values` has only the values whose source is the project (`ValueSource.valueRemote`). | `getAll()` of Firebase has the defaults of the app too. Without the filter, a parameter that the project lacks would read a value of Firebase in place of its default. |
| Its `updates` passes on `onConfigUpdated` of Firebase, after which `appRemoteConfig` calls `activate()`. | Values that change in the middle of a screen come only through `appRemoteConfig`. So no other code calls `activate()`, `fetch()` or `fetchAndActivate()` of the package. |

## What the Firebase project needs

The app needs two APIs of Google in the Firebase project of each flavor:

| API | What for |
| --- | --- |
| Firebase Remote Config API (`firebaseremoteconfig.googleapis.com`) | The fetches of the app. |
| Firebase Remote Config Realtime API (`firebaseremoteconfigrealtime.googleapis.com`) | The values that you publish while the app runs. It answers 403 while it is disabled. |

If the API keys of the Android and iOS apps of the project have API restrictions, in the Google Cloud console under APIs & Services, Credentials, each key must allow both APIs, or the fetches and the real-time updates of the app fail.

Firebase gives a project 100,000 fetches a day for free. Beyond them, a project on the Spark plan is throttled, and one on the Blaze plan is billed for each fetch; see the [prices of Remote Config](https://firebase.google.com/docs/remote-config/pricing). An app without parameters fetches nothing.

## Privacy

Each fetch sends the Firebase installation ID of the app on the device, an identifier of the installation. Declare it in the App Privacy details of App Store Connect and in the Data safety form of Google Play. Both stores also ask for a link to the privacy policy of the app, in the listing of the app and inside the app: name Firebase Remote Config there among the services that the app sends data to.

Every value reaches every installation of the app that fetches it, where anyone can read it. So never put a secret into remote config, such as a key or a password.

## Finish the setup

The app reads its options of Firebase from the configuration that `tool/configure_firebase.dart` writes for each flavor, as with every Firebase module; see [Firebase](https://doc.saymyframe.com/guides/firebase.md).

Then create the parameters of the app in Remote Config of the Firebase project of each flavor, with the names, the types and the defaults that the section "Remote config" of the README lists. No command can do it without replacing what you set in the console, so it is a step by hand of an app with parameters, which the README lists under "Finish the setup":

| Step | What you do |
| --- | --- |
| [`firebase_remote_config.parameters`](https://doc.saymyframe.com/setup/firebase-remote-config-parameters.md) | Create the parameters of the app in Firebase Remote Config, and publish the changes. |

An app without parameters has no such step. Until you create a parameter, the app reads its default.

## In tests

The package has no platform side for tests. A test that runs `bootstrap()` answers its messages to the platform itself, with `setMockMessageHandler` of `TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger`, on the channels `dev.flutter.pigeon.firebase_remote_config_platform_interface.FirebaseRemoteConfigHostApi.<method>` in the codec of the package, and on the event channel `plugins.flutter.io/firebase_remote_config_updated`. It also starts Firebase, which needs a mock of its platform side; see [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md). The guide for coding agents of the app says the same. A test of the values alone needs none of this: it gives `appRemoteConfig.start()` a service of its own; see [Remote config](https://doc.saymyframe.com/guides/remote-config.md#in-tests).

Whichever module provides remote config, a parameter reads its default until the backend gives a value, the start waits at most a second, and what was activated is still there at the next start. SMF's CI checks this in running apps with the role. For this module, over a stand-in for the platform side that answers the messages of the plugin, it checks what the service sends to Firebase on Android and on iOS, that its values never have a default, and that a value that the project publishes while the app runs becomes active through `appRemoteConfig`. On a device, it checks that a fetch from the Firebase project of the app succeeds. On an Android emulator, it publishes a value of a parameter of its own in the Firebase project of CI, which has to reach the running app through the real-time updates. See [Contributing](https://doc.saymyframe.com/contributing.md#the-roles-at-run-time) and [starting the apps](https://doc.saymyframe.com/contributing.md#starting-the-apps).

## Choosing it

`smf create` asks which module provides the remote config of the app, and offers None as well. Enter takes this module. To choose it without the question:

```bash
smf create my_app -m firebase_remote_config
```

Firebase comes with it:

```text
Adding firebase_core: a dependency of firebase_remote_config.
Adding flutter_core: the only provider of the app entry role, which every app needs.
```

Such an app has no parameter yet, so it asks Firebase nothing and lists no step by hand.

[`update_required`](https://doc.saymyframe.com/modules/update-required.md) requires the remote config role, so with it `smf create` adds this module without a question while it is the only one that provides remote config.

After generation, `firebase_core` asks whether to configure the flavor `dev`, as for every app with Firebase; see [Firebase](https://doc.saymyframe.com/guides/firebase.md).
