Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

Cache Manager

A tidy utility to handle cache of your flutter app like a Boss. It provides support for both iOS and Android platforms (offcourse).

Installing

dependencies:
  cache_manager: ^<latest_version>

Awesome Features

Cache Utilities

CacheManagerUtils.conditionalCache({key, valueType, actionIfNull, actionIfNotNull})

CacheManagerUtils.cacheTextBuilder(textStyle, cacheKey})

Read cache

ReadCache.getJson(key) : Get JSON stored as cache.

ReadCache.getString(key) : Get string stored as cache.

ReadCache.getBool(key) : Get boolean stored as cache.

ReadCache.getInt(key) : Get integer stored as cache.

ReadCache.getDouble(key) : Get double stored as cache.

Write cache

WriteCache.setJson(key,value) : Set JSON as cache.

WriteCache.setString(key,value) : Set a string as cache.

WriteCache.setInt(key,value) : Set an integer as cache.

WriteCache.setBool(key,value) : Set a boolean as cache.

WriteCache.setDouble(key,value) : Set a double as cache.

WriteCache.setListString(key,value) : Set a List of string as cache.

Delete cache

DeleteCache.deleteKey(key,[takeAction]) : Delete the cache and perform an action when cache is deleted(Optional).

Example : Login flow with caching userID

//Attached function in a login view
Future<String?> login({
    required BuildContext context,
    required String email,
    required String password,
  }) async {
    try {
      var userId = await _authenticationService.login(
          context: context, email: email, password: password);
      await WriteCache.setString(key: "cache", value: userId!);
    } catch (e) {
      print(e); //Do something if error occurs
   }
 }
 
//Splash view
import 'dart:async';
import 'package:cache_manager/cache_manager.dart';
import 'package:flutter/material.dart';

class SplashView extends StatefulWidget {
  @override
  _SplashViewState createState() => _SplashViewState();
}

class _SplashViewState extends State<SplashView> {
  Future initiateCache() async {
    return CacheManagerUtils.conditionalCache(
        key: "cache",
        valueType: ValueType.StringValue,
        actionIfNull: () {
          Navigator.of(context).pushNamed(AppRoutes.LoginRoute);
        },
        actionIfNotNull: () {
          Navigator.of(context).pushNamed(AppRoutes.HomeRoute);
        });
  }

  @override
  void initState() {
    Timer(Duration(seconds: 1), initiateCache);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("demo app"),
      ),
    );
  }
}

//Home view
import 'package:cache_manager/core/cache_manager_utils.dart';
import 'package:cache_manager/core/delete_cache_service.dart';
import 'package:flutter/material.dart';

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CacheManagerUtils.cacheTextBuilder(
            textStyle: TextStyle(color: Colors.white), cacheKey: "cache"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          DeleteCache.deleteKey(
              "cache", Navigator.of(context).pushNamed(AppRoutes.LoginRoute));
        },
      ),
    );
  }
}

Download Cache Manager app source code on GitHub


https://github.com/abhishh1/cache_manager
3 forks.
14 stars.
3 open issues.

Recent commits:

Exit mobile version