Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class JsonCacheInfoRepository extends CacheInfoRepository
Future<void> _readFile(File file) async {
_cacheObjects.clear();
_jsonCache.clear();
if (await file.exists()) {
final hasContent = await file.length() > 0;
if (await file.exists() && hasContent) {
try {
final jsonString = await file.readAsString();
final json = jsonDecode(jsonString) as List<dynamic>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ void main() {
});
});

test('Open repository should not report error when file is empty',
() async {
final originalOnError = FlutterError.onError;
FlutterError.onError = (FlutterErrorDetails details) {
throw details.exception;
};

final tempFile = File('${Directory.systemTemp.path}/test_empty.json');
await tempFile.create();
expect(await tempFile.length(), 0);

final repository = JsonCacheInfoRepository.withFile(tempFile);
await repository.open();

expect(await repository.getAllObjects(), isEmpty);

await tempFile.delete();
FlutterError.onError = originalOnError;
});

group('Exist and delete', () {
test('New repository does not exist', () async {
var repository = JsonCacheInfoRepository.withFile(File(path));
Expand Down