Home > OS >  Open dialog at a certain amount of launches of the Flutter App
Open dialog at a certain amount of launches of the Flutter App

Time:01-10

I have an App and i want it to open a dialog after the user launched the app 5 times. How can i make it possible?

The package I'm using to open the dialog is in_app_review.

import 'package:in_app_review/in_app_review.dart';

final InAppReview inAppReview = InAppReview.instance;

 if(userHasLaunchedAppFiveTimes){ 
      if (await inAppReview.isAvailable()) {
         inAppReview.requestReview();
      }
    }

CodePudding user response:

You have to use persistent storage to achieve this. You can use this package: shared_preferences Use the below code to increment nbTimesLaunched each time user launches app.

SharedPreferences prefs = await SharedPreferences.getInstance();
int nbTimesLaunched = (prefs.getInt('nbTimesLaunched') ?? 0)   1;
await prefs.setInt('nbTimesLaunched', nbTimesLaunched);

And then when you want to show the dialog:

SharedPreferences prefs = await SharedPreferences.getInstance();
int nbTimesLaunched = prefs.getInt('nbTimesLaunched') ?? 1;
if (nbTimesLaunched == 5) {
    // show dialogue
}

UPDATE

You can just put the first part where you increment the count inside main. make sure to call the WidgetsFlutterBinding.ensureInitialized() or else you'll get an error.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int nbTimesLaunched = (prefs.getInt('nbTimesLaunched') ?? 0)   1;
  await prefs.setInt('nbTimesLaunched', nbTimesLaunched);
  runApp(const MyApp());
}
  •  Tags:  
  • Related