How to Refresh App State When Date Changes?

1

I have an app that relies heavily on the date. I want to make sure the app refreshes and is up-to-date when it's a new day. I have been using the WidgetsBindingObserver class but I'm not sure if I'm doing something wrong.

Basically if a user visit the app on August 20th, closes it or puts their phone/tablet to sleep. Then comes back on August 21st, the app is not refreshing and the date text I have in the app is not refreshing to the new date. The below code does not seem to be changing the date text when the app is reawakened on a new day. Any help is greatly appreciated.

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/widgets.dart';


class HomePage extends StatefulWidget {
  @override
  _HomePage createState() => _HomePage();
  }

class _HomePage extends State<HomePage> with WidgetsBindingObserver {
  String _formattedDate;
  String _formattedDay;
  final now = DateTime.now();
  SharedPreferences sharedPreferences;

  @override
  void initState(){
      super.initState();
      _setupDateDisplay().then((_formattedDate){

      });

      WidgetsBinding.instance.addObserver(this);
  }

    Widget build(BuildContext context) {
      return new Scaffold(
          backgroundColor: Color(0xfffde8d3).withOpacity(0.5),
          body: Center(
            child: Text('$_formattedDate ($_formattedDay)',style: TextStyle(color: Color(0xffffffff), fontSize: 16.0)),
          ),
        );
      }

  @override
  void didChangeAppLifecycleState(final AppLifecycleState state) {
    switch(state) {
      case AppLifecycleState.resumed:
        print("APP STATE RESUMED");
        setState(() {
          _refreshContent();
        });
        break;
      case AppLifecycleState.inactive:
        print("APP STATE INACTIVE");
        break;
      case AppLifecycleState.paused:
        print("APP STATE PAUSED");
        break;
      case AppLifecycleState.suspending:
        print("APP STATE SUSPENDING");
        break;
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    print("APP IS DISPOSING");
    super.dispose();
  }

  void _refreshContent() {
      _setupDateDisplay().then((_formattedDate){

      });
  }

    Future<String> _setupDateDisplay() async {
      _formattedDate = DateFormat.yMMMMd("en_US").format(now);
      _formattedDay = DateFormat.EEEE("en_US").format(now);
      setState(() {
        _formattedDate = DateFormat.yMMMMd("en_US").format(now);
        _formattedDay = DateFormat.EEEE("en_US").format(now);
      });
      return _formattedDate;
    }

}
flutter
dart
asked on Stack Overflow Aug 31, 2019 by kelsheikh

1 Answer

1

This is happening because you are storing DateTime.now() in a class field. As a result, everytime you use now, it is going to have the same value, which is the date in which the state was created.

Instead, make now a local variable so it is going to be instantiated every time the method is called:

Future<String> _setupDateDisplay() async {
  final now = DateTime.now();

  _formattedDate = DateFormat.yMMMMd("en_US").format(now);
  _formattedDay = DateFormat.EEEE("en_US").format(now);
  setState(() {
    _formattedDate = DateFormat.yMMMMd("en_US").format(now);
    _formattedDay = DateFormat.EEEE("en_US").format(now);
  });
  return _formattedDate;
}
answered on Stack Overflow Aug 31, 2019 by Hugo Passos • edited Jul 26, 2020 by Hugo Passos

User contributions licensed under CC BY-SA 3.0