Skyost / RateMyApp

This plugin allows to kindly ask users to rate your app if custom conditions are met (eg. install time, number of launches, etc...).
https://pub.dev/packages/rate_my_app
MIT License
265 stars 104 forks source link

Initialize with provider #119

Closed desmeit closed 2 years ago

desmeit commented 2 years ago

I want to use the rateMyApp instance everywhere in my App:

import 'package:flutter/material.dart';
import 'package:rate_my_app/rate_my_app.dart';

class RatingService extends ChangeNotifier {
  RatingState ratingState = RatingState.loading;
  RateMyApp rateMyApp = RateMyApp(
    preferencesPrefix: 'rateMyApp_',
    minDays: 0, // Show rate popup on first day of install.
    minLaunches:
        2, // Show rate popup after 5 launches of app after minDays is passed.
    googlePlayIdentifier: 'XXX',
    appStoreIdentifier: 'XXX',
  );

  RatingService() {
    initRating();
  }

  Future<void> initRating() async {

    await rateMyApp.init();

    ratingState = RatingState.available;
    notifyListeners();
  }
}

enum RatingState {
  loading,
  available,
  notAvailable,
}

and

 return MultiProvider(
        providers: [
          ChangeNotifierProvider<RatingService>(
            create: (context) => RatingService(),
          ),
...

But I get the following error:

LateInitializationError: Field 'minimumDate' has not been initialized.

Is there a way to achieve this?

desmeit commented 2 years ago

solved it with

var rating = context.watch<RatingService>();

if (rating != null && rating.ratingState == RatingState.available) {
      print("ratingState: " + rating.ratingState.toString());
      print("rating: " + rating.rateMyApp.shouldOpenDialog.toString());
}