0

So What I am working on is an app that would allow you to make a schedule for charging your phone at a specific time using a device. one of the things that i was working on is displaying a prompt upon startup that would ask the user if they would like to change the settings. This prompt, whether the user agreed or not, should appear only once when the app is booted up. Here is the issue

P.S these are the necessary variables

late SharedPreferences sp;
final firstTime = 'is_first_loaded';
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Future<void> _displayPercentagePromptOnStartup(BuildContext context) async {
     sp = await _prefs;
    
    bool? isFirstTime = sp.getBool(isFirstBuild);
    log(isFirstTime.toString());
    if (isFirstTime == null) {
      showDialog(
        // Display Dialog Prompt for setting the percentage upon bootup
        context: context,
        builder: (context) => Center(
          child: AlertDialog(
            title: Text(
              'Notice',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Theme.of(context).primaryColor,
              ),
            ),
            content: Container(
              height: 115,
              width: MediaQuery.of(context).size.width,
              child: Text(
                "It appears that the setting you created the last time this application was opened it was at ${limitC.limitPercentage}%\n\nDo you wish to change it now?",
                style: const TextStyle(
                    fontSize: 16,
                    fontFamily: 'Roboto',
                    fontWeight: FontWeight.bold),
              ),
            ),
            actionsAlignment: MainAxisAlignment.center,
            actions: <Widget>[
              TextButton(
                  onPressed: (() {
                    Get.back();
                    sp.setBool(isFirstBuild, false);
                  }),
                  child: Text('Cancel',
                      style: TextStyle(
                        color: Theme.of(context).primaryColor,
                      ))),
              TextButton(
                  onPressed: () {
                    onTapSetLimitListTile();
                    sp.setBool(isFirstBuild, false);
                  },
                  child: Text('Set',
                      style: TextStyle(
                        color: Theme.of(context).primaryColor,
                      ))),
            ],
          ),
        ),
      );
    }
  }

and this is my initState()

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

         Future.delayed(Duration.zero, () => _displayPercentagePromptOnStartup(context));

    _initCurrentSetPoint();
    _battery.batteryState.then(_updateBatteryState);
    _batteryStateSubscription =
        _battery.onBatteryStateChanged.listen(_updateBatteryState);
  }

The issue that i have with this is that the prompt never appears. now i got the shared preferences from this link How to show a popup on app start in Flutter. While the particular post says that if i want it to show up once everytime the app is booted up i would have to clear the shared preferences but the issue is, i do not know where to reset it. if anyone could help me out that would be much appreciated

MZX
  • 1
  • 3

0 Answers0