Skip to content

Migrate to NNBD #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 27, 2021
Merged
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
12 changes: 12 additions & 0 deletions flutter_cache_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## [3.0.0-nullsafety.3] - 2021-03-26
* Add null-check on id in removeFile

## [3.0.0-nullsafety.2] - 2021-03-22
* Fix sqflite warning

## [3.0.0-nullsafety.1] - 2021-03-02
* Bug fix for NonStoringObjectProvider.

## [3.0.0-nullsafety.0] - 2021-02-25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's drop the -nullsafety.0 bits. We can release stable now!

See https://medium.com/dartlang/preparing-the-dart-and-flutter-ecosystem-for-null-safety-e550ce72c010

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but besides null safety being stable we also make sure that this package is stable.
All tests are stil broken and @ryanheise also found a bug. So we can change 3.0.0-beta.0, but that doesn't really help much.

* Migration to nullsafety.

## [2.1.2] - 2021-03-09
* Update dependencies
* Bug fix for JsonCacheInfoRepository when file is corrupted.
Expand Down
2 changes: 0 additions & 2 deletions flutter_cache_manager/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ analyzer:
unused_local_variable: warning
dead_code: warning
invalid_override_of_non_virtual_member: error
enable-experiment:
- extension-methods

linter:
rules:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// ignore_for_file: lines_longer_than_80_chars

import 'package:url_launcher_web/url_launcher_web.dart';

import 'package:flutter_web_plugins/flutter_web_plugins.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FileInfoWidget extends StatelessWidget {
),
Padding(
padding: const EdgeInsets.all(10.0),
// ignore: deprecated_member_use
child: RaisedButton(
child: const Text('CLEAR CACHE'),
onPressed: clearCache,
Expand Down
12 changes: 6 additions & 6 deletions flutter_cache_manager/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
name: example
description: A new Flutter project.
publish_to: none

version: 1.0.0+1

environment:
sdk: ">=2.10.2 <3.0.0"

dependencies:
baseflow_plugin_template:
git:
url: git://github.com/Baseflow/baseflow_plugin_template.git
ref: v1.0.0
cupertino_icons: ^1.0.2
flutter:
sdk: flutter

flutter_cache_manager:
path: ../
cupertino_icons: ^1.0.2
url_launcher: ^5.4.11
baseflow_plugin_template:
git:
url: git://github.com/Baseflow/baseflow_plugin_template.git
ref: v1.0.0

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#include "generated_plugin_registrant.h"

#include <url_launcher_windows/url_launcher_plugin.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

Expand Down
71 changes: 38 additions & 33 deletions flutter_cache_manager/lib/src/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,32 @@ class CacheManager implements BaseCacheManager {
/// The [fileService] can be used to customize how files are downloaded. For example
/// to edit the urls, add headers or use a proxy. You can also choose to supply
/// a CacheStore or WebHelper directly if you want more customization.
CacheManager(Config config) {
_config = config;
_store = CacheStore(config);
CacheManager(Config config)
: _config = config,
_store = CacheStore(config) {
_webHelper = WebHelper(_store, config.fileService);
}

@visibleForTesting
CacheManager.custom(
Config config, {
CacheStore cacheStore,
WebHelper webHelper,
}) {
_config = config;
_store = cacheStore ?? CacheStore(config);
CacheStore? cacheStore,
WebHelper? webHelper,
}) : _config = config,
_store = cacheStore ?? CacheStore(config) {
_webHelper = webHelper ?? WebHelper(_store, config.fileService);
}

Config _config;
final Config _config;

/// Store helper for cached files
CacheStore _store;
final CacheStore _store;

/// Get the underlying store helper
CacheStore get store => _store;

/// WebHelper to download and store files
WebHelper _webHelper;
late final WebHelper _webHelper;

/// Get the underlying web helper
WebHelper get webHelper => _webHelper;
Expand All @@ -74,8 +73,8 @@ class CacheManager implements BaseCacheManager {
@override
Future<File> getSingleFile(
String url, {
String key,
Map<String, String> headers,
String? key,
Map<String, String>? headers,
}) async {
key ??= url;
final cacheFile = await getFileFromCache(key);
Expand All @@ -95,12 +94,12 @@ class CacheManager implements BaseCacheManager {
@override
@Deprecated('Prefer to use the new getFileStream method')
Stream<FileInfo> getFile(String url,
{String key, Map<String, String> headers}) {
{String? key, Map<String, String>? headers}) {
return getFileStream(
url,
key: key,
withProgress: false,
).map((r) => r as FileInfo);
).where((r) => r is FileInfo).cast<FileInfo>();
}

/// Get the file from the cache and/or online, depending on availability and age.
Expand All @@ -116,18 +115,17 @@ class CacheManager implements BaseCacheManager {
/// might be outdated and a new file is being downloaded in the background.
@override
Stream<FileResponse> getFileStream(String url,
{String key, Map<String, String> headers, bool withProgress}) {
{String? key, Map<String, String>? headers, bool withProgress = false}) {
key ??= url;
final streamController = StreamController<FileResponse>();
_pushFileToStream(
streamController, url, key, headers, withProgress ?? false);
_pushFileToStream(streamController, url, key, headers, withProgress);
return streamController.stream;
}

Future<void> _pushFileToStream(StreamController streamController, String url,
String key, Map<String, String> headers, bool withProgress) async {
String? key, Map<String, String>? headers, bool withProgress) async {
key ??= url;
FileInfo cacheFile;
FileInfo? cacheFile;
try {
cacheFile = await getFileFromCache(key);
if (cacheFile != null) {
Expand Down Expand Up @@ -166,7 +164,9 @@ class CacheManager implements BaseCacheManager {
///Download the file and add to cache
@override
Future<FileInfo> downloadFile(String url,
{String key, Map<String, String> authHeaders, bool force = false}) async {
{String? key,
Map<String, String>? authHeaders,
bool force = false}) async {
key ??= url;
var fileResponse = await _webHelper
.downloadFile(
Expand All @@ -182,13 +182,13 @@ class CacheManager implements BaseCacheManager {
/// Get the file from the cache.
/// Specify [ignoreMemCache] to force a re-read from the database
@override
Future<FileInfo> getFileFromCache(String key,
Future<FileInfo?> getFileFromCache(String key,
{bool ignoreMemCache = false}) =>
_store.getFile(key, ignoreMemCache: ignoreMemCache);

///Returns the file from memory if it has already been fetched
@override
Future<FileInfo> getFileFromMemory(String key) =>
Future<FileInfo?> getFileFromMemory(String key) =>
_store.getFileFromMemory(key);

/// Put a file in the cache. It is recommended to specify the [eTag] and the
Expand All @@ -201,15 +201,19 @@ class CacheManager implements BaseCacheManager {
Future<File> putFile(
String url,
Uint8List fileBytes, {
String key,
String eTag,
String? key,
String? eTag,
Duration maxAge = const Duration(days: 30),
String fileExtension = 'file',
}) async {
key ??= url;
var cacheObject = await _store.retrieveCacheData(key);
cacheObject ??= CacheObject(url,
key: key, relativePath: '${Uuid().v1()}.$fileExtension');
cacheObject ??= CacheObject(
url,
key: key,
relativePath: '${const Uuid().v1()}.$fileExtension',
validTill: DateTime.now().add(maxAge),
);

cacheObject = cacheObject.copyWith(
validTill: DateTime.now().add(maxAge),
Expand All @@ -233,17 +237,18 @@ class CacheManager implements BaseCacheManager {
Future<File> putFileStream(
String url,
Stream<List<int>> source, {
String key,
String eTag,
String? key,
String? eTag,
Duration maxAge = const Duration(days: 30),
String fileExtension = 'file',
}) async {
key ??= url;
var cacheObject = await _store.retrieveCacheData(key);
cacheObject ??= CacheObject(url,
key: key,
relativePath: '${Uuid().v1()}'
'.$fileExtension');
relativePath: '${const Uuid().v1()}'
'.$fileExtension',
validTill: DateTime.now().add(maxAge));

cacheObject = cacheObject.copyWith(
validTill: DateTime.now().add(maxAge),
Expand All @@ -267,8 +272,8 @@ class CacheManager implements BaseCacheManager {
@override
Future<void> removeFile(String key) async {
final cacheObject = await _store.retrieveCacheData(key);
if (cacheObject != null) {
await _store.removeCachedFile(cacheObject);
if (cacheObject?.id != null) {
await _store.removeCachedFile(cacheObject!);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ abstract class BaseCacheManager {
/// returned from the cache there will be no progress given, although the file
/// might be outdated and a new file is being downloaded in the background.
Stream<FileResponse> getFileStream(String url,
{String key, Map<String, String> headers, bool withProgress});
{String? key, Map<String, String>? headers, bool withProgress});

///Download the file and add to cache
Future<FileInfo> downloadFile(String url,
{String key, Map<String, String> authHeaders, bool force = false});
{String? key, Map<String, String>? authHeaders, bool force = false});

/// Get the file from the cache.
/// Specify [ignoreMemCache] to force a re-read from the database
Future<FileInfo> getFileFromCache(String key, {bool ignoreMemCache = false});
Future<FileInfo?> getFileFromCache(String key, {bool ignoreMemCache = false});

///Returns the file from memory if it has already been fetched
Future<FileInfo> getFileFromMemory(String key);
Future<FileInfo?> getFileFromMemory(String key);

/// Put a file in the cache. It is recommended to specify the [eTag] and the
/// [maxAge]. When [maxAge] is passed and the eTag is not set the file will
Expand All @@ -61,8 +61,8 @@ abstract class BaseCacheManager {
Future<File> putFile(
String url,
Uint8List fileBytes, {
String key,
String eTag,
String? key,
String? eTag,
Duration maxAge = const Duration(days: 30),
String fileExtension = 'file',
});
Expand All @@ -77,8 +77,8 @@ abstract class BaseCacheManager {
Future<File> putFileStream(
String url,
Stream<List<int>> source, {
String key,
String eTag,
String? key,
String? eTag,
Duration maxAge = const Duration(days: 30),
String fileExtension = 'file',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import '../config/config.dart';
class DefaultCacheManager extends CacheManager with ImageCacheManager {
static const key = 'libCachedImageData';

static DefaultCacheManager _instance;
static final DefaultCacheManager _instance = DefaultCacheManager._();
factory DefaultCacheManager() {
_instance ??= DefaultCacheManager._();
return _instance;
}

Expand Down
Loading