britannio / in_app_review

A Flutter plugin for showing the In-App Review/System Rating pop up on Android, IOS, and MacOS. It makes it easy for users to rate your app.
327 stars 82 forks source link

Question: Code example to load on particular page #87

Closed Aravin closed 1 year ago

Aravin commented 1 year ago

Can you please share the Code example to load on a particular page on load?

can we use it in initState()?

britannio commented 1 year ago

https://github.com/britannio/in_app_review#requestreview demonstrates how to use the plugin.

This example attempts to show the popup after the first frame has been displayed.

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      final InAppReview inAppReview = InAppReview.instance;

      if (await inAppReview.isAvailable()) {
        inAppReview.requestReview();
      }
    });
  }
britannio commented 1 year ago

ChatGPT actually provides an excellent response:

https://sharegpt.com/c/9OPOact

britannio commented 1 year ago

https://www.phind.com/search?cache=8564ce05-5124-486c-a91e-48e2cf8b9d96 provides a less direct response but with links to sources such as https://stackoverflow.com/questions/65399583/use-of-in-app-review-package-in-flutter

Aravin commented 1 year ago

Thanks @britannio

Aravin commented 1 year ago

And my code looks like this

@override
  void initState() {
    saveOnboardingSettings();
    super.initState();

    (<T>(T? o) => o!)(WidgetsBinding.instance).addPostFrameCallback((_) async {
      try {
        final isAvailable = await _inAppReview.isAvailable();

        setState(() {
          _availability = isAvailable && !Platform.isAndroid
              ? Availability.available
              : Availability.unavailable;
          _inAppReview.requestReview();
        });
      } catch (_) {
        setState(() => _availability = Availability.unavailable);
      }
    });
  }
britannio commented 1 year ago

Nice. Bear in mind that you may be able to replace (<T>(T? o) => o!)(WidgetsBinding.instance) with WidgetsBinding.instance as it was just a precaution to ensure the example app compiled on older versions of Flutter. Also, you likely don't need the _availability variable, or if you do, it shouldn't contain the Platform.isAndroid case.