# Store reviews and feedback

A generated app can ask its users for a review in the store, and let them write to you. Each part is a role of its own, with a module that provides it:

| Part | Role | Module | What it brings |
| --- | --- | --- | --- |
| Store reviews | Store reviews | [`in_app_review`](https://doc.saymyframe.com/modules/in-app-review.md) | `appReview`, which asks the App Store or Google Play for its own review dialog when a review is due, and opens the page of the app in its store. |
| Feedback | Feedback | [`email_feedback`](https://doc.saymyframe.com/modules/email-feedback.md) | `appFeedback`, which opens a new mail to you in the mail app of the device. |

```bash
smf create my_app -m home,settings,in_app_review,email_feedback
```

`smf create` asks "Store reviews: which module provides it?" and "Feedback: which module provides it?", and Enter takes these modules, the first provider of each. Both roles use the settings screen: in an app with one, the review role adds the row "Rate the app", and the feedback role the row "Write to us".

| File | From | What it holds |
| --- | --- | --- |
| `lib/core/review/app_review.dart` | the store reviews role | `appReview`, `ReviewPolicy`, `googlePlayId` and `initReview()` |
| `lib/core/review/review_service.dart` | the store reviews role | `ReviewService`, the interface of a provider. The code of the app does not use it. |
| `lib/core/review/review_setting.dart` | the store reviews role, in an app with a settings screen | `ReviewSetting`, the row "Rate the app" |
| `lib/core/review/in_app_review_service.dart` | `in_app_review` | The reviews on in_app_review |
| `lib/core/feedback/app_feedback.dart` | the feedback role | `appFeedback` |
| `lib/core/feedback/feedback_service.dart` | the feedback role | `FeedbackService`, the interface of a provider. The code of the app does not use it. |
| `lib/core/feedback/feedback_setting.dart` | the feedback role, in an app with a settings screen | `FeedbackSetting`, the row "Write to us" |
| `lib/core/feedback/email_feedback_service.dart`, `email_feedback_mail.dart` and `email_feedback_sheet.dart` | `email_feedback` | The mail, its link, and the sheet with the address where no mail app opens it |

Neither role registers anything in a DI container: the code of the app uses `appReview` and `appFeedback`, two top-level variables.

## Ask for a review

The app asks for a review only where its code calls `appReview.requestIfDue()`. Call it at a natural break, such as when the user has finished a task:

```dart
await saveNote(note);
unawaited(appReview.requestIfDue());
```

When a review is due, the call asks the App Store or Google Play for its own review dialog, and the app shows nothing around it. It returns nothing: the store tells no app whether the dialog showed or what the user did, so nothing in the app may depend on it. It never throws. A call while a request is on its way gets that request.

Never call it from a button, and never after a question of your own, such as "Do you like the app?": Google Play forbids both, and the App Store allows no prompt but its own. The App Store also asks apps not to request a review when the user launches them, so the role asks for nothing on its own. No module of SMF calls `appReview.requestIfDue()`: the contract harness reports one that does.

## When a review is due

A review is due once each of these holds:

- This launch is at least the third of the app (`ReviewPolicy.launches`), and three days have passed since its first launch (`ReviewPolicy.sinceFirstLaunch`).
- The app did not ask yet, or 14 days have passed since it last asked (`ReviewPolicy.between`), also in a new version.
- The version of the app is known, and is not the version in which the app last asked. So the app asks at most once a version.
- The store of the device can show the dialog.
- The user is in no flow of a guard of the routes, such as the onboarding or a sign-in. In an app with guards, a gate that does not allow keeps the user in its flow, and so does a screen of the flow of any guard on top, which the role learns from a listener of the screen that it gives the router. See [Guards](https://doc.saymyframe.com/concepts/guards.md).

Change the numbers in `ReviewPolicy` of `lib/core/review/app_review.dart`. Keep `launches` at 2 or more, so that the app never asks on its first launch.

The app counts its launches and remembers its last request in the preferences, under the keys `review.launches`, `review.first_launch`, `review.asked_at` and `review.asked_version`, so the role requires the [preferences](https://doc.saymyframe.com/guides/preferences.md). `restoreReview`, a restorer of the preferences, reads them before the first frame, and `initReview()`, which `bootstrap()` awaits in its last phase, counts the launch. Write nothing under the keys `review.*`. A request is saved once the store took it. An error of the store saves nothing and counts as no dialog, so a later call asks again.

## When the dialog shows

The store decides whether the dialog shows:

- iOS shows it in a debug build each time, never in TestFlight, and at most three times in 365 days.
- Google Play shows it only in an app that it installed, such as from an internal test track. [`in_app_review`](https://doc.saymyframe.com/modules/in-app-review.md#see-the-dialog-on-android) tells how to see it before the app is published.
- On a device without the review service of Google Play, such as an emulator without the Play Store, the store cannot take the request and the app records nothing, so the next call asks again, also in the same launch.
- On a device with the Play Store, for an app that Google Play did not install, such as a local build, the request completes and shows nothing, and the app counts it as a request, so it does not ask again in that version of the app.

## The page of the app in its store

`appReview.openStorePage()` opens the page of the app in its store, outside the app, and `appReview.openStorePage(review: true)` the page where the user writes a review. It returns whether an app of the device opened the page, and never throws, so any code opens the store page without knowing a provider. [`update_required`](https://doc.saymyframe.com/modules/update-required.md) opens it this way from its button "Update".

| Platform | The page |
| --- | --- |
| Android | The Google Play page of `googlePlayId`, the application id of the flavor for a release, `prod`, also from a build of another flavor, whose own id has no page there. Change `googlePlayId` in `lib/core/review/app_review.dart` if you change the application id of the app. |
| iOS | The App Store page of `REVIEW_APP_STORE_ID` in `.env`, the Apple ID of the app in App Store Connect, such as `1234567890`. With `review: true`, the page has `?action=write-review`. |
| Any other platform | None. |

`appReview.storePage()` gives the address, and `appReview.hasStorePage` tells whether there is one. On iOS there is none while `REVIEW_APP_STORE_ID` is empty, or has a value that is not all digits, and a debug build prints which key to fill in. Copying the ID into `.env` is a step by hand; see [Copy the App Store ID](https://doc.saymyframe.com/setup/review-app-store-id.md).

## The row "Rate the app"

In an app with a settings screen, the role adds `ReviewSetting` in `lib/core/review/review_setting.dart`, the row "Rate the app". A tap opens the page for a review, and never asks for the review dialog: Google Play forbids the dialog from a button and points a button to the page. When no app of the device opens the page, a snack bar says so.

The row shows only where the app has a store page, through `reviewSettingShows()`, its function of `SettingsEntry.showIf`. So on iOS it hides until `.env` has the App Store ID. The screen asks the function each time it builds; see [How settings work](https://doc.saymyframe.com/concepts/settings.md#the-settings-screen).

| Text | English | Ukrainian |
| --- | --- | --- |
| The title of the row | Rate the app | Оцінити застосунок |
| What the snack bar says | Couldn't open the store. | Не вдалося відкрити магазин. |

## Feedback

`appFeedback.open(context)` lets the users of the app write to you. Call it from a button, with a `BuildContext` below the root of the app:

```dart
TextButton(
  onPressed: () => appFeedback.open(context),
  child: const Text('Write to us'),
)
```

It opens the way to write of the provider, such as a new mail in the mail app, and completes with `true` once that way opened, without waiting for the user to send anything. It completes with `false` when the way could not open, after the provider showed the user another way to reach you if it has one, such as the address to copy. It never throws: an error of the provider goes to the handler of the errors of Flutter, such as that of a crash reporter, and counts as `false`. A call while another is on its way does nothing and completes with `false`, and so does a call with a context that is no longer mounted.

`appFeedback.available` tells whether the provider has what it needs, such as your address, and does not change while the app runs. While it is `false`, `open` does nothing, so hide a button of your own then. With [`email_feedback`](https://doc.saymyframe.com/modules/email-feedback.md), it is `false` until `EMAIL_FEEDBACK_ADDRESS` in `.env` has an address; see [Put the email address into .env](https://doc.saymyframe.com/setup/email-feedback-address.md).

Each provider adds to what reaches you the name, the version, the build number and the flavor of the app and its platform, so that you know what the user ran. The section of the provider in the README of the app tells what it sends and how.

### The row "Write to us"

In an app with a settings screen, the role adds `FeedbackSetting` in `lib/core/feedback/feedback_setting.dart`, the row "Write to us" («Написати нам» in Ukrainian). A tap calls `appFeedback.open(context)`, and the row shows nothing of its own: where the way cannot open, the provider shows its own way to reach you. A second tap while the first is on its way does nothing.

The row shows only while `appFeedback.available` holds, through `feedbackSettingShows()`, its function of `SettingsEntry.showIf`. It reads nothing else.

In an app without a settings screen, call `appFeedback.open(context)` from a button of your own, such as on a screen of help.

## Reviews and feedback apart

The way to write is there whatever the user did with the review dialog of a store. Google Play forbids any question before or around that dialog, so never send the happy users to the store and the unhappy ones to "Write to us".

A way to write inside the app does not replace the Support URL that App Store Connect asks of every app, a page where its users reach you (App Store Review Guideline 1.5).

## Privacy

- Through the review dialog, Google Play collects the rating and the text of the review; see [`in_app_review`](https://doc.saymyframe.com/modules/in-app-review.md#see-the-dialog-on-android).
- What a user writes leaves the device through the provider of feedback. With `email_feedback`, it leaves only in a mail that the user sends from their own mail app. A provider that sends it through a service of its own, such as a form, collects data: declare it in App Privacy and in Data safety.
- Both stores require a link to a privacy policy in the listing of the app and inside the app.

## The README and the guide for coding agents

The README of the app has the sections "Store reviews" and "Feedback" of the roles, and those of the modules, "in_app_review" and "Feedback by email". Its section "Finish the setup" lists the steps by hand of the app, such as [`review.app_store_id`](https://doc.saymyframe.com/setup/review-app-store-id.md) and [`email_feedback.address`](https://doc.saymyframe.com/setup/email-feedback-address.md).

The [guide for coding agents](https://doc.saymyframe.com/guides/coding-agents.md) of the app tells agents, under "Store reviews", where the code asks for a review and what it must never do around the dialog, and under "Feedback", how a button opens the way to write and hides while it is not available.

## In tests

A test of the reviews creates an `AppReviewController` of its own, with a clock of its own and a function that gives the App Store ID, gives it the preferences with `restore()`, and starts it with a service of its own, which scripts what a provider answers. To see what the next launch of the app finds, a test runs `initPreferences()` and `initReview()` again, after it saved a state of its own under the keys of the role through the preferences. A test of a screen starts `appReview` itself with `appReview.start()` and a service of its own.

A test of a screen with a button that opens the way to write gives `appFeedback.use()` a service of its own, and puts back the service of the provider, `createFeedbackService()`, when it ends. The code of the app never calls `appFeedback.use()`.

The mocks of every provider keep to one convention in the tests that SMF's CI runs in the apps, so the tests of the roles hold with each:

- For the reviews, the store of the device can show its dialog, a request completes at once and shows nothing, a page opens, and the version of the app is `1.0.0`.
- For the feedback, the provider has what it needs, such as its address in `.env`, the device has what the provider opens, such as a mail app, and asked whether it has it, the device says no, as Android 11 and later say.

Over these, SMF's CI checks in running apps that no review is due before the third launch or three days after the first, that the app asks once a version and not again within 14 days, that a refused request saves nothing, that a launch asks for nothing on its own, and that nothing new shows on the screen around a request. It checks that "Rate the app" opens exactly the Google Play page and never asks for the dialog, and that `appFeedback.open(context)` runs one call at a time and turns an error of the provider into `false`. See [Contributing](https://doc.saymyframe.com/contributing.md#the-roles-at-run-time).
