skip to Main Content

I want to show my popup survey only once in the app lifecycle once the user completes the survey, meaning when the user restarts the app, the popup should not appear again. What happens now is if user restarts the app, the popup still shows again.

Help will be appreaciated

my code for popup

     void showGenexPopup() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final GenexViewModel genexViewModel =
          Provider.of<GenexViewModel>(context, listen: false);

      if (!genexViewModel.videoTrigger) {
        genexViewModel.showSurvey(
          context: context,
          pageId: GenexPageId.SearchPage,
          duration: const Duration(seconds: 5),
        );
      }
    });
  }

where I am calling it:

@override
  void initState() {
    super.initState();
    showGenexPopup();
  }

I am calling the popup in a webview, I do not know if it will have any effect on what I want to achieve.

Is it possible for me to achieve this?

3

Answers


  1. Chosen as BEST ANSWER

    Thanks guys for your responses. You really assisted me in getting to my answer using sharedPreference.

    This is the approach that worked for me:

     void showGenexPopup(BuildContext context) async {
       SharedPreferences prefs = await SharedPreferences.getInstance();
       bool? isFirstLoaded =
           prefs.getBool('is_first_loaded');
       if (isFirstLoaded == null) {
         WidgetsBinding.instance.addPostFrameCallback((_) {
           final GenexViewModel genexViewModel =
               Provider.of<GenexViewModel>(context, listen: false);
    
           if (!genexViewModel.videoTrigger) {
             genexViewModel.showSurvey(
               context: context,
               pageId: GenexPageId.SearchPage,
               duration: const Duration(seconds: 5),
             );
           }
         });
         prefs.setBool('is_first_loaded', false);
       }
     }
    

  2. As @amir_a14 suggests u should use shared preferences where you save a key that indicate that the user took this survey once he complete it. Then before you call show survey check this key in the SharedPref and if this value is settled to the right value or exist at all

    which will be in general like this:

    final prefs = await SharedPreferences.getInstance();
    
    void showGenexPopup()async {
    //get the key in sharedPref here 
    final didUserTookSurvey = prefs.getBool('repeat');
    
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final GenexViewModel genexViewModel =
          Provider.of<GenexViewModel>(context, listen: false);
    
        //check the key in sharedPref here 
      if (!genexViewModel.videoTrigger && !(didUserTookSurvey??false)) {
        genexViewModel.showSurvey(
          context: context,
          pageId: GenexPageId.SearchPage,
          duration: const Duration(seconds: 5),
          //Call this when the user completed the survey successfully
          onComplete:onCompleteSurvey;
        }
       });
      }
    
    onCompleteSurvey(){
      try{
         await prefs.setBool('didUserTookSurvey', true);
      }catch(e){
       Log(e.toString());       
      }
    }
    
    Login or Signup to reply.
  3. Use SharedPrefrence library

     import 'package:shared_preferences/shared_preferences.dart';
    
    //showGenexPopup
     void showGenexPopup() async {
    
     final prefs = await SharedPreferences.getInstance();
     bool isSeen=await prefs.setBool('isSeen')??false;
       
      if(isSeen)
      {
      WidgetsBinding.instance.addPostFrameCallback((_) {
      final GenexViewModel genexViewModel =
          Provider.of<GenexViewModel>(context, listen: false);
    
      if (!genexViewModel.videoTrigger) {
        genexViewModel.showSurvey(
          context: context,
          pageId: GenexPageId.SearchPage,
          duration: const Duration(seconds: 5),
        );
         }
       await prefs.setBool('isSeen', true);
       });
      }
       }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search