Skip to content

Some widgets' nullOk parameters are inconsistently applied. #68637

Description

@shyndman

The Actions widget has a number of static methods with a nullOk parameter, which can be used to enable or disable the throwing of errors when the caller's requested object was not found.

Turns out, this parameter is only considered when asserts are enabled. This feels a little iffy to me, as the documentation makes no mention of how the contract can change depending on environment.

Request: Improve docs (and maybe the name of the param, since it's sort of a debug only thing), or alternatively normalize behavior between asserts-enabled and disabled builds.

Affected methods:

  • Actions.find
  • Actions.of
  • Actions.handler
  • Actions.invoke

Thanks!

@gspencergoog

Activity

  1. gspencergoog commented on Oct 20, 2020

    @gspencergoog
    Contributor

    The name of the parameter is consistent across many different similar static methods, not just Actions, and has the same behavior there (e.g. MediaQuery.of, Focus.of, Localizations.localeOf, etc.). And yes, it only applies to builds with assertions on, but that was always its intent.

    I agree that the behavior should be mentioned more explicitly in the docs.

  2. added
    d: api docsIssues with https://api.flutter.dev/
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Oct 20, 2020
  3. shyndman commented on Oct 20, 2020

    @shyndman
    ContributorAuthor

    I took a look at all of those, and it appears they are designed to throw regardless of whether asserts are enabled (although Localizations.localeOf might throw an NPE as opposed to a well-formed FlutterError).

  4. gspencergoog commented on Oct 20, 2020

    @gspencergoog
    Contributor

    Ahh, I see what you mean. OK, I can see making those throw a FlutterError on a non-assert build.

    We seem to actually be quite inconsistent with our methods that take nullOk as a parameter, perhaps we should try and normalize that across the framework.

  5. shyndman commented on Oct 20, 2020

    @shyndman
    ContributorAuthor

    Couldn't hurt. :)

    Thanks!

  6. changed the title [-]Actions widget's nullOk parameters only considered with asserts enabled[/-] [+]Some widgets' nullOk parameters are inconsistently applied.[/+] on Oct 20, 2020
  7. gspencergoog commented on Oct 20, 2020

    @gspencergoog
    Contributor

    I'm converting this issue over to cover all the static methods that take a nullOk parameter.

    We have a bunch of static member functions that have a nullOk argument to them, but we're really inconsistent with how we apply it. Sometimes it will return null in some circumstances even if nullOk is false (e.g. CupertinoDynamicColor.resolve()), sometimes it only works when asserts are enabled (e.g. Actions.find), and other times it throws a FlutterError in release mode (e.g. MediaQuery.of()). Sometimes I think the checks are assert-only because of performance, though, so we can't just make them all throw FlutterError all the time. At the least, we should probably update the API docs for each one that describes what behavior is expected.

  8. shyndman commented on Oct 20, 2020

    @shyndman
    ContributorAuthor

    Thanks Greg.

    When the time comes, I'd love to be CC'd on that PR if you get a chance.

  9. gspencergoog commented on Oct 20, 2020

    @gspencergoog
    Contributor

    Sure, will do.

  10. gspencergoog commented on Oct 20, 2020

    @gspencergoog
    Contributor

    Related: #67163

  11. gspencergoog commented on Oct 23, 2020

    @gspencergoog
    Contributor

    Here's a design doc for doing this: https://flutter.dev/go/eliminating-nullok-paramters (I know, I'm fixing the name typo in the go link).

  12. added
    P1High-priority issues at the top of the work list
    on Oct 23, 2020
  13. self-assigned this
    on Oct 23, 2020
  14. gspencergoog commented on Oct 23, 2020

    @gspencergoog
    Contributor

    FYI, in discussions about this, @Hixie was of the opinion (and I agree) that eventually the FlutteError exceptions here should be thrown only from inside of asserts, since we eventually want to remove debug messages from release builds for size reasons (and because they're not really actionable there anyhow).

  15. shyndman commented on Oct 26, 2020

    @shyndman
    ContributorAuthor

    Makes sense. Would exceptions without messages be thrown in the non-assert case?

  16. gspencergoog commented on Nov 5, 2020

    @gspencergoog
    Contributor

    @shyndman Yes, and I added wording to that effect in the docs.

  17. gspencergoog commented on Dec 4, 2020

    @gspencergoog
    Contributor

    This is all done, except for Localizations.localeOf, since a client (flutter_svg) is still using it. Once dnfield/flutter_svg#443 lands, and then we remove the nullOk from Localizations.localeOf, this issue can be closed.

  18. shyndman commented on Dec 4, 2020

    @shyndman
    ContributorAuthor

    Thanks!

    It's one of those things where a few people will never realize the thanks they owe — the bug they didn't have to track down. :)

  19. goderbauer commented on Jan 25, 2021

    @goderbauer
    Member

    There's one more nullOK param remaining after all changes linked from this bug are submitted: https://master-api.flutter.dev/flutter/widgets/Actions/invoke.html

  20. gspencergoog commented on Jan 25, 2021

    @gspencergoog
    Contributor

    I'll fix the Actions.invoke nullOk before closing.

  21. shyndman commented on Jan 26, 2021

    @shyndman
    ContributorAuthor

    Thanks Greg

  22. github-actions commented on Aug 6, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  23. locked as resolved and limited conversation to collaborators on Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: error messageError messages from the Flutter frameworkd: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions