0

I am trying to update the two variables when app resumes, the variables are minutes and hours. Right now when i resume the app the values don't get updated.

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
    dateTimeNow = DateTime.parse('${prefs.getString('startTime')}');
    startedDateTime = DateTime.now();

    minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
    hours = startedDateTime.difference(dateTimeNow).inHours;

    if (minutes < 0) {
      minutes = 0;
    }
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        print("Inactive");
        break;
      case AppLifecycleState.paused:
        print("Paused");
        break;
      case AppLifecycleState.resumed:
        print('resumed');
        setState(() { // trying to updated
          minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
          hours = startedDateTime.difference(dateTimeNow).inHours;
        });
        break;
    }
  }
LearnFlutter
  • 214
  • 2
  • 22
  • I recommend viewing the accepted answer here: https://stackoverflow.com/questions/49869873/flutter-update-widgets-on-resume – Ayren King Mar 24 '22 at 15:20

2 Answers2

1

It seems that you are not updating the startedDateTime value. You only set it during the first initialization of the state but you are not updating the value later. Meaning, even after updating your values with didChangeAppLifecycleState, the startedDateTime is still the same, hence the minutes or hours values remain the same.

Try to do something like this:

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  super.didChangeAppLifecycleState(state); // <-- Add this as well
  switch (state) {
    <...>
    case AppLifecycleState.resumed:
      print('resumed');
      setState(() {
        // trying to updated
        startedDateTime = DateTime.now(); // <-- Update the value
        minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
        hours = startedDateTime.difference(dateTimeNow).inHours;
      });
      break;
  }
}
mkobuolys
  • 4,499
  • 1
  • 11
  • 27
0

Kindly add super.didChangeAppLifecycleState(state); at the end of didChangeAppLifecycleState() method as like this:

 void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        print("Inactive");
        break;
      case AppLifecycleState.paused:
        print("Paused");
        break;
      case AppLifecycleState.resumed:
        print('resumed');
        setState(() { // trying to updated
          minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
          hours = startedDateTime.difference(dateTimeNow).inHours;
        });
        break;
    }
        super.didChangeAppLifecycleState(state);
  }

And also override the dispose method:

void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    super.dispose();
  }
ramkumar
  • 226
  • 3
  • 9