Skip to content
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

Migrate to null safety #443

Merged
merged 5 commits into from
Feb 27, 2021
Merged

Migrate to null safety #443

merged 5 commits into from
Feb 27, 2021

Conversation

miDeb
Copy link
Contributor

@miDeb miDeb commented Feb 26, 2021

Hi! I noticed that all dependencies of this package are ready for null safety, so I figured I could quickly migrate chewie to null safety.

I am aware that this is a fairly big change, but I hope that most of it is straightforward.
You'll notice that I have removed some null checks, simply because the checked value could never be null and the check would therefore always return true. For example, _latestValue (in Material-/CupertinoControls) is set right away in methods called from initState, so with null safety it can be marked as late and other methods don't have to do unnecessary null checks.

I tested various features in the example, and everything seems to work. I didn't migrate the example itself yet though, because auto_orientation does not yet support null safety (I opened a pull request to resolve this).

There seems to be one test in the example, which fails for me before and after this patch. Do you rely on manual testing only?

@nstrelow
Copy link
Collaborator

BAAM, thanks a lot @miDeb .
I actually planned to look into null safety today. Pretty amazing that I now find a PR for it.
Will check you PR thoroughly and report back.

About auto_orientation, I wanted to check if it is actually still needed. It has not been updated since Dec 2019, so maybe flutter now supports its functionality.

@miDeb
Copy link
Contributor Author

miDeb commented Feb 27, 2021

About auto_orientation, I wanted to check if it is actually still needed. It has not been updated since Dec 2019, so maybe flutter now supports its functionality.

Looks like you're right, in fact after simply removing the dependency rotating still seems to still work.

ChewieController chewieController;
AnimationController playPauseIconAnimationController;
late VideoPlayerController controller;
ChewieController get chewieController => _chewieController!;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why can we not use a late ChewieController here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is the logic in didChangeDependencies. This is the only place where _chewieController is used directly, and this is required because it tries to compare the old ontroller with the new controller. That would not work with late the very first time a controller is set, because there is no old controller and a LateInitializationError would be triggered.

The way I wrote this the rest of the widget (especially build methods) don't have to do a null check every time, but didChangeDependecies can have access to a nullable _chewieController.

Hope that makes sense :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice solution then.
Last idea I had was using initState, but then we cannot get the correct context for inheritedWidget.

Could you add a comment for the getter: Something like // We know that _chewieController is set in didChangeDependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that will make it clearer for readers. Done.

@miDeb
Copy link
Contributor Author

miDeb commented Feb 27, 2021

I have now migrated the example as well; note that I removed auto_rotate.dart as it was not used and did not seem to show additional features.

@nstrelow
Copy link
Collaborator

Very awesome !!!

@@ -37,17 +37,19 @@ class _ChewieDemoState extends State<ChewieDemo> {
void dispose() {
_videoPlayerController1.dispose();
_videoPlayerController2.dispose();
_chewieController.dispose();
_chewieController?.dispose();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is not strictly related to the migration, but it makes the test pass.

Comment on lines -47 to +52
await _videoPlayerController1.initialize();
_videoPlayerController2 = VideoPlayerController.network(
'https://assets.mixkit.co/videos/preview/mixkit-a-girl-blowing-a-bubble-gum-at-an-amusement-park-1226-large.mp4');
await _videoPlayerController2.initialize();
await Future.wait([
_videoPlayerController1.initialize(),
_videoPlayerController2.initialize()
]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The changes here are also to make the test pass again.

@nstrelow
Copy link
Collaborator

Awesome PR!
Thanks for bringing null safety to chewie.

Hope this will make people happy :D

@nstrelow nstrelow merged commit 2214c93 into fluttercommunity:master Feb 27, 2021
@miDeb miDeb deleted the nnbd branch February 27, 2021 13:45
@nstrelow nstrelow mentioned this pull request Feb 27, 2021
@nstrelow
Copy link
Collaborator

Published as 1.0.0

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.

2 participants