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

Migrate to NNBD #278

merged 21 commits into from
Mar 27, 2021

Conversation

Jjagg
Copy link
Contributor

@Jjagg Jjagg commented Feb 11, 2021

This migrates the library to NNBD.
For now this only migrates the flutter_cache_manager package and its tests.
There's 2 test failures I have yet to resolve, so I'm submitting as a draft for now.

Heads up that build_runner needs to be run before running tests because of changes to Mockito to support NNBD.

Fixes #253.

🤔 Checklist before submitting

  • All projects build
  • Follows style guide lines (code style guide)
  • Relevant documentation was updated
  • Rebased onto current develop

_memCache.remove(key);
} else {
_memCache[key] = cacheObject;
}
Copy link
Contributor Author

@Jjagg Jjagg Feb 11, 2021

Choose a reason for hiding this comment

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

Behavior change here. Seemed to make sense to me to remove null values and make the cached items non-nullable.

@@ -22,5 +22,5 @@ class FileInfo extends FileResponse {

/// Validity date of the file. After this date the validity is not guaranteed
/// and the CacheManager will try to update the file.
final DateTime validTill;
final DateTime? validTill;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if this actually should be null. Had to use a lot of bangs because I made it nullable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to make it required.

when(provider.open()).thenAnswer((_) => Future.value(true));
when(provider.update(any, setTouchedToNow: anyNamed('setTouchedToNow')))
.thenAnswer((realInvocation) => Future.value(0));
when(provider.updateOrInsert(any)).thenAnswer((realInvocation) async =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can let Mockito return null for unstubbed methods, but I did not enable that because it can cause runtime casting errors with NNBD. Downside is all methods need to be stubbed explicitly.

}),
returnValue: Stream<_i10.FileResponse>.empty())
as _i7.Stream<_i10.FileResponse>);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You might prefer not having this generated file checked in, LMK.

@hoc081098
Copy link
Contributor

hoc081098 commented Feb 18, 2021

Dart beta has been already release, please publish it. https://medium.com/dartlang/preparing-the-dart-and-flutter-ecosystem-for-null-safety-e550ce72c010

@Jjagg
Copy link
Contributor Author

Jjagg commented Feb 18, 2021

Thanks for reviewing @hoc081098, I made some changes.

@renefloor
Copy link
Contributor

@Jjagg I wanted to start with this today, but found your PR.
I think we should have way more non-optionals and throw an exception if something is not found.
For example

Future<FileInfo?> getFile(String key, {bool ignoreMemCache = false})

This should just be Future<FileInfo?> imo. What do you prefer? Shall I comment on your PR, push changes to your branch or start my own branch?

I don't mind any breaking changes, as long as it is documented. That's why this will be a new major version.

@Jjagg
Copy link
Contributor Author

Jjagg commented Feb 19, 2021

IMO a caching library should not throw an exception when an item is retrieved that is not in the cache. But I don't have too strong of an opinion on it and I don't use this library directly, so it doesn't matter much to me.

I've given you access to my fork. Feel free to commit directly to this branch or branch off and set up your own pull request/commit directly to the main branch. Either would be a little faster than you commenting and me making the changes :)

@renefloor
Copy link
Contributor

I'm also not 100% sure about the best way, I think both null and an exception are fine if documented well.

@ryanheise
Copy link

From Effective Dart:

Dart uses exceptions when an error occurs in your program.

And from the Language Tour:

Exceptions are errors indicating that something unexpected happened

I agree that for a caching library, a cache miss is not an error but something to be expected.

@acoutts
Copy link

acoutts commented Feb 23, 2021

Hi- the force unwrap here is causing a runtime error for me:
https://github.com/Jjagg/flutter_cache_manager/blob/nnbd/flutter_cache_manager/lib/src/storage/file_system/file_system_io.dart#L16

../../.pub-cache/git/flutter_cache_manager-439af02c4c059986f46101a003ac8ebd20658ef7/flutter_cache_manager/lib/src/storage/file_system/file_system_io.dart:16:23: Warning: Operand of null-aware operation '!' has type 'Directory' which excludes null.
 - 'Directory' is from 'dart:io'.
    var path = p.join(baseDir!.path, key);
                      ^

../../.pub-cache/git/flutter_cache_manager-439af02c4c059986f46101a003ac8ebd20658ef7/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart:196:20: Warning: Operand of null-aware operation '!' has type 'Directory' which excludes null.
 - 'Directory' is from 'dart:io'.

      directory = (await getApplicationSupportDirectory())!;
                  ^

@acoutts
Copy link

acoutts commented Feb 23, 2021

Screen Shot 2021-02-23 at 2 00 20 PM

@Jjagg
Copy link
Contributor Author

Jjagg commented Feb 23, 2021

@acoutts
Looks like they changed behavior in path_provider for the the stable 2.0.0 release: flutter/plugins#3582.
Are you overriding that dependency by any chance?

I'll update dependencies to the stable version for packages where it's already available and fix that issue.

@acoutts
Copy link

acoutts commented Feb 23, 2021

I'm not overriding it no- i'm on 2.0.0-nullsafety.1.

@acoutts
Copy link

acoutts commented Feb 24, 2021

I was able to silence the error with this override:

dependency_overrides:
  path_provider: 2.0.0-nullsafety.1

# Conflicts:
#	flutter_cache_manager/lib/src/storage/cache_info_repositories/json_cache_info_repository.dart
@renefloor
Copy link
Contributor

In the latest stable of path_provider they changed the return type from Future<Directory?> to Future
flutter/plugins@bb3fc5a#diff-78e86d7326b605de0b626343cc28963ef7bff3587995640f6c1dddadbd7333ed

I upgraded the dependencies that have a stable version now.

Comment on lines +106 to +120
// test('Non-existing cacheFile should call to web', () async {
// var fileName = 'test.jpg';
// var fileUrl = 'baseflow.com/test';
// var fileKey = 'test1234';
// var validTill = DateTime.now().subtract(const Duration(days: 1));
//
// var config = createTestConfig();
// config.returnsCacheObject(fileUrl, fileName, validTill, key: fileKey);
//
// var cacheManager = TestCacheManager(config);
//
// var result = await cacheManager.getSingleFile(fileUrl, key: fileKey);
// expect(result, isNotNull);
// config.verifyDownloadCall();
// });
Copy link
Contributor

Choose a reason for hiding this comment

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

@Jjagg I couldn't get this test working, do you have any idea what's going wrong here? It looks like webhelper.downloadFile is not really called, but that is not mocked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What was the error message?

From your comment I think it's because of the changes to mockito with NNBD.
Mocks are generated with build_runner and by default all methods throw. Any method that is called should be mocked.

@renefloor renefloor marked this pull request as ready for review February 25, 2021 13:56
Copy link
Contributor

@kevmoo kevmoo left a comment

Choose a reason for hiding this comment

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

Thanks for this! Would love to get this landed. This is on our list of critical/important community packages!

Let me know if you need any help!

@@ -1,3 +1,7 @@
## [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.

@@ -1,26 +1,28 @@
name: flutter_cache_manager
description: Generic cache manager for flutter. Saves web files on the storages of the device and saves the cache info using sqflite.
version: 2.1.1
version: 3.0.0-nullsafety.0
Copy link
Contributor

Choose a reason for hiding this comment

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

drop the -nullsafety bit here. Should be ready for stable!

image: ^2.1.18

http: ^0.13.0
image: ^3.0.0-nullsafety.0
Copy link
Contributor

Choose a reason for hiding this comment

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

drop -nullsafety this is published stable now!

path: ^1.8.0
path_provider: ^2.0.0
pedantic: ^1.10.0
rxdart: ^0.26.0-nullsafety.1
Copy link
Contributor

Choose a reason for hiding this comment

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

drop -nullsafety this is published stable now!

Copy link
Contributor

Choose a reason for hiding this comment

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

cool!

@kevmoo
Copy link
Contributor

kevmoo commented Feb 28, 2021

Who has the power to merge and publish?

@Jjagg
Copy link
Contributor Author

Jjagg commented Feb 28, 2021

That would be @renefloor :)

@renefloor
Copy link
Contributor

@kevmoo it is already published as preview version: https://pub.dev/packages/flutter_cache_manager/versions/3.0.0-nullsafety.0

I try to migrate CachedNetworkImage this week and if I don't get any issues I'll merge and publish as stable.

@ryanheise
Copy link

In NonStoringObjectProvider, it defines:

  @override
  Future<CacheObject> get(String url) {
    return Future.value(null);
  }

But CacheObject is non-nullable.

The analyser didn't detect any problem statically (which is unfortunate since that should be the goal of null safety) but the documentation for Future.value, indicates that you will get a runtime exception:

If [value] is omitted or null, it is converted to FutureOr<T> by value as FutureOr<T>. If T is not nullable, then a non-null [value] must be provided, otherwise the construction throws.

@PcolBP
Copy link

PcolBP commented Mar 16, 2021

After upgrading sqflite to 2.0.0+3 , now getDatabasesPath returns non-nullable String no need to add null-aware operand.

image

In case when sqflite is upgraded and cacheManager stays at nullsafety.1 branch error occur:

image

@Jjagg
Copy link
Contributor Author

Jjagg commented Mar 16, 2021

I'm only getting a warning, so they might have changed the analyzer behavior. Nevertheless, doesn't hurt to upgrade sqflite.
EDIT: Oh, I thought you got an error because of the red text 😅

# Conflicts:
#	flutter_cache_manager/CHANGELOG.md
#	flutter_cache_manager/example/pubspec.yaml
#	flutter_cache_manager/pubspec.yaml
@davidmartos96
Copy link
Contributor

@Jjagg There is this issue as well in the NNBD version. #297
The id of the cache object can be null when reaching the removeFile function, so the unconditional bang fails.

@codecov
Copy link

codecov bot commented Mar 26, 2021

Codecov Report

Merging #278 (c6c6fb6) into develop (d9061bd) will decrease coverage by 1.37%.
The diff coverage is 72.26%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #278      +/-   ##
===========================================
- Coverage    76.11%   74.73%   -1.38%     
===========================================
  Files           21       21              
  Lines          674      669       -5     
===========================================
- Hits           513      500      -13     
- Misses         161      169       +8     
Impacted Files Coverage Δ
.../lib/src/cache_managers/default_cache_manager.dart 0.00% <0.00%> (ø)
...ter_cache_manager/lib/src/compat/file_fetcher.dart 77.77% <ø> (-12.23%) ⬇️
...utter_cache_manager/lib/src/config/_config_io.dart 12.50% <ø> (ø)
...cache_info_repositories/cache_object_provider.dart 0.00% <0.00%> (ø)
...info_repositories/non_storing_object_provider.dart 0.00% <ø> (ø)
...er/lib/src/storage/file_system/file_system_io.dart 0.00% <0.00%> (ø)
flutter_cache_manager/lib/src/web/queue_item.dart 100.00% <ø> (ø)
..._info_repositories/json_cache_info_repository.dart 90.62% <75.86%> (-2.16%) ⬇️
...lutter_cache_manager/lib/src/web/file_service.dart 87.87% <81.81%> (-6.57%) ⬇️
flutter_cache_manager/lib/src/cache_manager.dart 88.60% <83.33%> (+1.42%) ⬆️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c869918...c6c6fb6. Read the comment docs.

@renefloor renefloor merged commit 776fb13 into Baseflow:develop Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Null safety support
8 participants