skip to Main Content

I am developing a flutter app that user click the button and the counter increase by one.

I want to rest the value of the counter to zero after one day.

Can u please tell me is there anyway to achieve that?

Thank you

2

Answers


  1. You can use flutter_cache package
    https://pub.dev/packages/flutter_cache/

    import 'package:flutter_cache/flutter_cache.dart' as cache;
    
    void main() async {
      // create new cache.
      cache.remember('key', 'data');
      cache.write('key', 'data');
    
      // add Cache lifetime on create
      cache.remember('key', 'data', 120);
      cache.write('key', 'data', 120);
    
      // load Cache by key
      // return `defaultValue` if key not exists
      cache.load('key', 'defaultValue');
    
      // destroy single cache by key
      cache.destroy('key');
    
      // destroy all cache
      cache.clear();
    
      await cache.remember('key', () {
        return 'test'; // or logic fetching data from api;
      });
    
      // or
    
      await cache.remember('key', () => 'test');
    
      cache.remember('key', 'data', 120); // saved for 2 mins or 120 seconds
      cache.write('key', 'data', 120);
    
      // multi depth map datatype.
      cache.remember('key', {
        'name': 'Ashraf Kamarudin',
        'depth2': {
          'name': 'depth2',
          'depth3': {'name': 'depth3'}
        }
      });
    
      cache.load('key'); // will return data in map datatype.
    }
    
    Login or Signup to reply.
    • Is your requirement passive or active? If it is active, you can refer to the following code. If it is passive, you need to poll regularly
    import 'package:flutter/material.dart';
    
    class MyWidget extends StatefulWidget {
      const MyWidget({super.key});
    
      @override
      State<MyWidget> createState() => _MyWidgetState();
    }
    
    class _MyWidgetState extends State<MyWidget> with WidgetsBindingObserver {
      DateTime? _presedDate;
      var number = 0;
      @override
      void initState() {
        _presedDate = DateTime.now();
        super.initState();
      }
    
      @override
      void didChangeAppLifecycleState(AppLifecycleState lifeCycle) async {
        var _isForeground = (lifeCycle == AppLifecycleState.resumed);
        if (_isForeground) checkNumber();
        super.didChangeAppLifecycleState(lifeCycle);
      }
    
      void checkNumber() {
        var currenDate = DateTime.now();
        var isToday = DateUtils.isSameDay(_presedDate, currenDate);
        if (!isToday) {
          number == 0;
          setState(() {});
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
            child: Scaffold(
                body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('$number'),
              TextButton(
                  onPressed: () {
                    number++;
                    setState(() {});
                    checkNumber();
                  },
                  child: Text('incrementNumber')),
            ],
          ),
        )));
      }
    }
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search