Skip to content

Conversation

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Oct 26, 2020

Description

In the majority of cases we assume in the framework that Directionality.of returns a non-null value. This PR changes the API of that method to guarantee a non-null return value (and throw if no Directionality can be found) to clean up those call sides. Additionally, it adds a new Directionality.maybeOf method for those handful of cases, we a null return value is actually ok.

I think overall this leads to cleaner code because in the cases where you don't expect Directionality.of to return null you can remove the !.

Checklist

Before you create this PR, confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

@flutter-dashboard flutter-dashboard bot added f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. work in progress; do not review labels Oct 26, 2020
@google-cla google-cla bot added the cla: yes label Oct 26, 2020
@ghost

This comment has been minimized.

@goderbauer
Copy link
Member Author

Customer tests for flutter_svg are failing on this change.

@goderbauer
Copy link
Member Author

In google this change uncovered 6 buggy tests, which were accidentally passing with Directionality.of returning null even though they shouldn't. Fixes for that a purely in the test by adding a Directionality widget to them.

One widget in google3 needs to have Directionality.of replaced by Directionality.maybeOf.

@goderbauer
Copy link
Member Author

goderbauer commented Oct 27, 2020

@dnfield @Hixie @gspencergoog Curious to hear what you think about this change. I think overall it improves the readability of the code by removing a lot of ! in places where we just assume Directionality.of to be non-null (and it also now throws a nice error message in case somebody forgot to include a Directionality widget). However, it is a breaking change. Running this through our customer tests, I found a single instances that need to replace their usage of Directionality.of with the newly introduced Directionality.maybeOf API:

  • In flutter_svg, there is one valid call of Directionality.of that is legitimately ok to return null.

At the same time, this change also uncovered 7 buggy tests in google, where the test authors forgot to include a Directionality widget (even though they should have).

Overall, I am in favor of this change, but like I said, it is slightly breaking.

@dnfield
Copy link
Contributor

dnfield commented Oct 27, 2020

Some of our .of API has a nullOk flag, e.g. Brightness.of, MediaQuery.of. Would we want to apply similar refactors to those?

If not, I think we might want to just add a nullOk here to make it consistent.

If so, I think we should evaluate when it's ok for these to be null or not. It seems like we have API usages that expect this to be nullable. I'd probably be more in favor of adding some assert(s) to this so that tests have to opt in to a nullOk. I'm having trouble deciding whether this is really the right way to go - I can think of cases where you'd ask about the direcitionality of your context and be ok with it not existing, and other cases where you wouldn't. You can make that clear already by using ! in your call. I'm a little concerned about adding yet another way to deal with missing inherited widgets in the tree (e.g. always allow null, allow null if nullOk, allow null if maybeOf).

@goderbauer
Copy link
Member Author

@gspencergoog is working on removing the nullOk parameters from those other methods you mentioned. We will introduce maybeOf methods for those as well.

@gspencergoog
Copy link
Contributor

@Hixie
Copy link
Contributor

Hixie commented Oct 27, 2020

I'm in favour of making every .of non-nullable, and adding .maybeOf wherever it makes sense. We should create a single migration guide for the whole thing.

@gspencergoog
Copy link
Contributor

OK, I'm happy to expand mine to include all the others (I think I have most of them in my doc already).

@dnfield
Copy link
Contributor

dnfield commented Oct 27, 2020

SGTM. In that case I'm in favor of this approach.

@goderbauer
Copy link
Member Author

Looks like we are in agreement on this change. Here's my plan for executing it:

  1. I'll submit a PR that adds Directionality.maybeOf to Flutter, leaving Directionality.of unchanged for now.
  2. That change rolls into google3.
  3. We update flutter_svg to use maybeOf (and bump its minimum flutter version).
  4. The updated flutter_svg rolls into google.
  5. I update Directionality.of in flutter to be non-null.
  6. Profit!

The first step of this plan is #69117.

@gspencergoog I will add these APIs to https://flutter.dev/go/eliminating-nullok-parameters so we remember to include them in the migration guide.

@goderbauer
Copy link
Member Author

goderbauer commented Oct 28, 2020

This PR now represents step 5 of the plan in my previous post. Submitting this is blocked on submitting dnfield/flutter_svg#435 first.

Copy link
Contributor

@gspencergoog gspencergoog left a comment

Choose a reason for hiding this comment

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

32384589-a60f0e74-c078-11e7-9bc1-e5b5287aea9d

Looks like SVG customer test is still unhappy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe mention that in debug mode it throws an assertion, and in release mode throws an exception.

@goderbauer
Copy link
Member Author

Looks like SVG customer test is still unhappy.

Yeah, i still need to submit dnfield/flutter_svg#435 to make those happy.

@goderbauer goderbauer force-pushed the directionality-non-null branch from a78be9a to 23c66a4 Compare October 30, 2020 21:39
@fluttergithubbot fluttergithubbot merged commit f2a25c5 into flutter:master Oct 30, 2020
@goderbauer goderbauer deleted the directionality-non-null branch October 30, 2020 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants