Skip to content

Conversation

@goderbauer
Copy link
Member

Description

Adds Directionality.maybeOf (and uses it in the framework) in preparation of making Directionality.of non-nullable. Making it non-nullable is not part of this PR since we first need to migrate some customers (flutter_svg) to the new maybeOf API before we can do that.

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.

@google-cla google-cla bot added the cla: yes label Oct 27, 2020
@goderbauer goderbauer requested a review from dnfield October 27, 2020 16:26
@goderbauer goderbauer mentioned this pull request Oct 27, 2020
13 tasks
final ThemeData theme = Theme.of(context)!;
final ChipThemeData chipTheme = ChipTheme.of(context);
final TextDirection? textDirection = Directionality.of(context);
final TextDirection? textDirection = Directionality.maybeOf(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

Here and below: should this really be nullable? I understand it was, but it might help to have a comment here about why we're allowing this to be null instead of using .of.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ultimately, they are passed to EdgeInsetsGeometry.resolve, which is fine with a null text direction as long as the EdgeInsetsGeometry is not text direction aware. Similar with the other cases.

We actually have tests for these. If you make it non-null here, they will fail.

/// ```dart
/// TextDirection textDirection = Directionality.of(context);
/// ```
// TODO(goderbauer): Make this non-null when customers have upgraded to Directionality.maybeOf.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we start encouraging people to migrate by adding an assert here with a message saying to use maybeOf if the resolved direction is null?

Copy link
Member Author

Choose a reason for hiding this comment

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

That could be kinda spammy. I think we usually don't do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Won't that effectively be what users gets though when we actually make this non-null? But probably without the benefit of a message printing next to the error that they really should use the other variant...

Copy link
Member Author

Choose a reason for hiding this comment

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

We can make the error message link to TextDirection.maybeOf.

Copy link
Contributor

Choose a reason for hiding this comment

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

How does that work for compile time errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean? The assert added will be a runtime error.

Copy link
Member Author

Choose a reason for hiding this comment

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

We will basically just call https://master-api.flutter.dev/flutter/widgets/debugCheckHasDirectionality.html at the beginning of this method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed offline - most apps will get directionality from material/cupertinoapp, probably not worth worrying about outside of tests.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

LGTM

@goderbauer goderbauer added framework flutter/packages/flutter repository. See also f: labels. waiting for tree to go green labels Oct 27, 2020
@goderbauer goderbauer merged commit f5ce638 into flutter:master Oct 27, 2020
@goderbauer goderbauer deleted the maybeOfDirect branch October 27, 2020 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants