# Remote config

The remote config role gives an app values that a backend changes without a new version of the app, such as a limit, a switch for a feature or the lowest version that may run. Each value is a parameter with a type and a default, which the app reads while the backend gives none.

| Part | Role | Module | What it brings |
| --- | --- | --- | --- |
| The values | Remote config | [`firebase_remote_config`](https://doc.saymyframe.com/modules/firebase-remote-config.md) | `appRemoteConfig` in `lib/core/remote_config/app_remote_config.dart`, with values from Firebase Remote Config |
| The screen "Update required" | none | [`update_required`](https://doc.saymyframe.com/modules/update-required.md) | A screen in place of the app while its version is below the lowest version in remote config |

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

`firebase_remote_config` brings [`firebase_core`](https://doc.saymyframe.com/modules/firebase-core.md), which sets up Firebase for each flavor; see [Firebase](https://doc.saymyframe.com/guides/firebase.md). `update_required` requires the role, so with it `smf create` adds `firebase_remote_config` without a question.

## Read a value

`appRemoteConfig.value` is a `RemoteValues`, with a getter for each parameter of the app. A getter returns the value that the backend gave the parameter, as the type of the parameter takes it, or the default of the parameter while the backend gives none:

```dart
import 'core/remote_config/app_remote_config.dart';

final minimum = appRemoteConfig.value.updateRequiredMinimumVersion;
```

The file exports `RemoteValues`, so the code of the app imports it alone.

`appRemoteConfig` is a `ValueListenable<RemoteValues>`, which tells its listeners when the value of a parameter changes. A widget that follows a value reads it in `build` under a `ValueListenableBuilder`:

```dart
ValueListenableBuilder(
  valueListenable: appRemoteConfig,
  builder: (context, values, _) => Text(values.updateRequiredMinimumVersion),
)
```

Code that must not change on the screen reads the value once, such as in `initState`. The app has one controller, so create no other `AppRemoteConfigController`.

A parameter of each type takes the text of the backend this way, and reads its default for a text that its type does not take, which a debug build prints once:

| Type | Dart type | What it takes |
| --- | --- | --- |
| A text | `String` | Any text. |
| A Boolean | `bool` | `true` and `false`, in any case. |
| A whole number | `int` | What `int.tryParse` takes. |
| A decimal | `double` | A finite number that `double.tryParse` takes. |

## With a state manager

In an app with [`bloc`](https://doc.saymyframe.com/modules/bloc.md) or [`riverpod`](https://doc.saymyframe.com/modules/riverpod.md), the state manager follows the values too, with a bridge that reads `appRemoteConfig`. The bridge is read-only: only the backend changes the values.

| State manager | Bridge | File |
| --- | --- | --- |
| `riverpod` | `remoteConfigValuesProvider`, which a widget reads with `ref.watch` | `lib/core/state/shared_state_providers.dart` |
| `bloc` | `RemoteConfigValuesCubit`, which a widget reads with `context.watch` | `lib/core/state/shared_state_cubits.dart` |

A widget that needs one parameter selects it from the values, so that a change of another parameter does not rebuild it: `RemoteValues` compares by its values. With Riverpod:

```dart
final minimum = ref.watch(
  remoteConfigValuesProvider.select(
    (values) => values.updateRequiredMinimumVersion,
  ),
);
```

With BLoC:

```dart
final minimum = context.select(
  (RemoteConfigValuesCubit cubit) => cubit.state.updateRequiredMinimumVersion,
);
```

## When the values come

The values never hold the start of the app. `bootstrap()` awaits `initRemoteConfig()` in its platform phase, which makes active what the last fetch got, which the device keeps, also without the network, and waits for that at most a second (`AppRemoteConfigController.startWait`). Values that come later still become the values of the app.

| When | What the app does |
| --- | --- |
| The start | Makes active what the device kept from the last fetch. |
| Right after the start, in the background | Asks the backend for new values and makes them active at once, also while a screen shows. Code that runs right after `bootstrap()` reads what the start made active. |
| Each time the user comes back to the app | Asks again. The provider asks its backend at most every 12 hours in a release build, and each time in a debug build, so a value that you change shows at once while you develop. |
| The backend publishes a value while the app runs | Where the provider tells of such values, as `firebase_remote_config` does with the real-time updates of Firebase, the app makes it active at once. |

An app without parameters asks the backend nothing and listens to nothing: it starts asking with its first parameter. An error of the backend keeps the values, prints in a debug build, and reaches no handler of the uncaught errors of the app.

## The parameters

The section "Remote config" of the README of the app lists its parameters, each with its type, its default, its getter and the module that reads it. A module gives the role a parameter for each value that its code reads, with a name in lower snake_case that starts with the id of the module, such as `update_required_minimum_version` of `update_required`, a type, a default and a description of one line. The getter is the name in lowerCamelCase, such as `updateRequiredMinimumVersion`. The code of a module reads only its own parameters and those of the modules it depends on. [Extending SMF](https://doc.saymyframe.com/extending.md) shows how a module of your own does it.

To add a parameter of your own, give it a default in `RemoteValues.defaults` and a getter in `lib/core/remote_config/remote_values.dart`:

```dart title="lib/core/remote_config/remote_values.dart"
  static const Map<String, Object> defaults = {
    'update_required_minimum_version': '',
    'promo_banner_enabled': false,
  };

  // ...

  /// Whether the start screen shows the promo banner.
  bool get promoBannerEnabled => _values['promo_banner_enabled']! as bool;
```

Then create it in the backend of the provider with the same name and type. With `firebase_remote_config`, that is Remote Config of the Firebase project of each flavor; see [the guide of the step](https://doc.saymyframe.com/setup/firebase-remote-config-parameters.md).

## Never a secret

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.

## Ask for an update

[`update_required`](https://doc.saymyframe.com/modules/update-required.md) gives the app the parameter `update_required_minimum_version`, the lowest version of the app that may run, empty by default. While the version of the app is below it, the screen "Update required" takes the place of the app, and its button opens the page of the app in its store, through the store reviews role; see [Store reviews and feedback](https://doc.saymyframe.com/guides/reviews-and-feedback.md).

To ask the users of old versions to update:

1. Publish the new version in both stores, and wait until every user can get it.
2. In the backend of remote config, set `update_required_minimum_version` to the new version, such as `1.4.0`. With Firebase, publish the change in Remote Config of the project of each flavor that needs it.
3. The screen shows at the next start of an old version, and in an app that runs once the new value comes.

The version of the app is its version name, such as `1.4.0` of `version: 1.4.0+12` in `pubspec.yaml`, and the two compare number by number, so `1.10.0` is above `1.9.2`. The screen is hard until the user taps Update, and then it also offers Not now, which lets the user in for this launch. So a lowest version set too early costs a user one trip to the store and locks nobody out. Lower the lowest version or empty it, and the user is back where they were.

- Raise the lowest version only when the old versions really cannot work any more, such as after a change of your backend that they do not understand.
- Never raise it above the last version that the oldest system that the app supports can install: a user on that system can update to nothing newer.
- Where the stores differ, give each platform its own lowest version. With Firebase, a condition of the platform in the console gives a parameter a value for Android and another for iOS.
- On iOS the app has a store page only with the App Store ID in `.env`, so without it the screen never shows there.

An app that stays offline never learns a new lowest version, and a user may stay on an old version with Not now. So only the backend of the app can really refuse an old version, such as by the version that each request names.

## In tests

A test of the values gives `appRemoteConfig.start()` a service of its own, which scripts what a provider answers, and runs `initRemoteConfig()` again to see what the next launch of the app finds. Such a service implements `RemoteConfigService` of `lib/core/remote_config/remote_config_service.dart`:

```dart
final class FixedRemoteConfig implements RemoteConfigService {
  FixedRemoteConfig(this.values);

  @override
  final Map<String, String> values;

  @override
  Future<void> open(
    Map<String, Object> defaults, {
    required Duration fetchInterval,
    required Duration fetchTimeout,
  }) async {}

  @override
  Future<bool> activate() async => false;

  @override
  Future<void> fetch() async {}

  @override
  bool get liveUpdates => false;

  @override
  Stream<void> get updates => const Stream.empty();
}
```

```dart
await appRemoteConfig.start(
  FixedRemoteConfig({'update_required_minimum_version': '2.0.0'}),
);
```

A test that brings the user back to the app may go through any state of the lifecycle: the controller looks only at `AppLifecycleState.resumed`.

A test of the screen "Update required" gives `appRemoteConfig.start()` and `appReview.start()` services of its own, and `updateStatus.start(review: ...)` an `AppReviewController` of its own, such as one with an App Store ID that it scripts. It runs `updateStatus.start()` again for the next launch. A test of another screen needs nothing: the default of the parameter is empty, so the app runs.

A test that runs `bootstrap()` starts the provider too, so it needs a mock of its platform side; see [`firebase_remote_config`](https://doc.saymyframe.com/modules/firebase-remote-config.md#in-tests).
