Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
740 stars 429 forks source link

_readFile in json_cache_info_repository.dart silently fails if jsonString is empty or corrupted in Linux desktop #262

Closed SteveLamont closed 3 years ago

SteveLamont commented 3 years ago

🐛 Bug Report

If the file ~/local/share/{appname}/libCachedImageData.json is empty or corrupted, the function jsonDecode() throws an error, which can cause the _package cached_networkimage, for instance, to malfunction.

I have added a try-catch block as follows

  try {

    var json = jsonDecode(jsonString) as List<dynamic>;
    for (var element in json) {
      if (element is! Map<String, dynamic>) continue;
      var map = element as Map<String, dynamic>;
      var cacheObject = CacheObject.fromMap(map);
      _jsonCache[cacheObject.id] = map;
      _cacheObjects[cacheObject.key] = cacheObject;
    }

  } catch ( e ) {

    print( "Hi there!" );

  }

` which at least tenatively works around the issue.

Expected behavior

Should properly recover from the error.

Reproduction steps

Delete ~/local/share/{appname}/libCachedImageData.json and create an empty file with

touch ~/local/share/{appname}/libCachedImageData.json

and then run your code.

Configuration

Version: 1.x

Platform: Linux, Ubuntu 20.10.

ericel commented 3 years ago

Getting a similar error. Never recovers unless cache is cleared somehow.

Screen Shot 2021-01-10 at 6 14 06 PM
renefloor commented 3 years ago

@SteveLamont and @ericel what do you think of PR #269?

SteveLamont commented 3 years ago

I'm cool with it if it fixes the issue. :-)