Skip to content

shrink the MediaQuery API surface - #194091

Closed
nate-thegrate wants to merge 7 commits into
flutter:masterfrom
nate-thegrate:deprecate-MediaQuery-maybeOf
Closed

nate-thegrate wants to merge 7 commits into
flutter:masterfrom
nate-thegrate:deprecate-MediaQuery-maybeOf

Conversation

@nate-thegrate

Copy link
Copy Markdown
Contributor

This pull request closes #183701 by deprecating several MediaQuery maybeOf methods and providing data-driven fixes.

Also in this PR:

  • moved MediaQuery-specific fixes in fix_widgets.yaml to fix_media_query.yaml
  • deprecated MediaQuery.displayCornerRadiiOf (see this comment)
  • deprecated debugCheckHasMediaQuery (no data-driven fix was added since it isn't possible to automatically remove the method call along with the surrounding assert)

`runApp` always inserts a `MediaQuery`, so most `maybeOf` methods are
unnecessary.

Nullable fields keep the maybe prefix. That includes the text-style
overrides and display corner radii, so displayCornerRadiiOf is
deprecated in favor of maybeDisplayCornerRadiiOf.
Also moves content in packages/flutter/test_fixes/widgets/ accordingly.
Update call sites whose null result only meant that no MediaQuery was
present. The non-maybe accessors already apply the same fallback, so
behavior is unchanged when an ancestor exists.
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

This pull request contains changes to Material or Cupertino, which are currently frozen in this repository.

Changes should be made in material_ui and/or cupertino_ui in the flutter/packages repository.

Please refer to #188444 for instructions.

@github-actions github-actions Bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages c: tech-debt Technical debt, code quality, testing, etc. f: cupertino labels Oct 9, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request deprecates debugCheckHasMediaQuery and the maybe... variant methods of MediaQuery (such as maybeOf and maybeSizeOf), as runApp now always introduces a MediaQuery. It updates documentation, removes obsolete assertions, and adds declarative data fixes to migrate users to the non-nullable ...Of methods. The review feedback suggests adding missing URIs for consistency in the fromView and boldTextOf migrations, and simplifying several ...Of methods in media_query.dart by directly calling _of instead of using unnecessary null-coalescing fallbacks.

Comment on lines 1832 to +1833
static TextScaler textScalerOf(BuildContext context) =>
maybeTextScalerOf(context) ?? TextScaler.noScaling;
_maybeOf(context, _MediaQueryAspect.textScaler)?.textScaler ?? TextScaler.noScaling;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since runApp always introduces a MediaQuery and there is no need to handle a missing MediaQuery (as established by the deprecation of the maybe... methods), we can simplify textScalerOf to use _of directly. This avoids the unnecessary null-coalescing fallback and makes the behavior consistent with other ...Of methods (like sizeOf, paddingOf, etc.) which throw if MediaQuery is missing.

  static TextScaler textScalerOf(BuildContext context) => _of(context, _MediaQueryAspect.textScaler).textScaler;
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

Comment on lines 1861 to +1863
static Brightness platformBrightnessOf(BuildContext context) =>
maybePlatformBrightnessOf(context) ?? Brightness.light;
_maybeOf(context, _MediaQueryAspect.platformBrightness)?.platformBrightness ??
Brightness.light;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since runApp always introduces a MediaQuery and there is no need to handle a missing MediaQuery (as established by the deprecation of the maybe... methods), we can simplify platformBrightnessOf to use _of directly. This avoids the unnecessary null-coalescing fallback and makes the behavior consistent with other ...Of methods (like sizeOf, paddingOf, etc.) which throw if MediaQuery is missing.

  static Brightness platformBrightnessOf(BuildContext context) => _of(context, _MediaQueryAspect.platformBrightness).platformBrightness;
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

Comment on lines +2094 to +2095
static bool highContrastOf(BuildContext context) =>
_maybeOf(context, _MediaQueryAspect.highContrast)?.highContrast ?? false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since runApp always introduces a MediaQuery and there is no need to handle a missing MediaQuery (as established by the deprecation of the maybe... methods), we can simplify highContrastOf to use _of directly. This avoids the unnecessary null-coalescing fallback and makes the behavior consistent with other ...Of methods (like sizeOf, paddingOf, etc.) which throw if MediaQuery is missing.

  static bool highContrastOf(BuildContext context) => _of(context, _MediaQueryAspect.highContrast).highContrast;
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

Comment on lines 2123 to +2124
static bool onOffSwitchLabelsOf(BuildContext context) =>
maybeOnOffSwitchLabelsOf(context) ?? false;
_maybeOf(context, _MediaQueryAspect.onOffSwitchLabels)?.onOffSwitchLabels ?? false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since runApp always introduces a MediaQuery and there is no need to handle a missing MediaQuery (as established by the deprecation of the maybe... methods), we can simplify onOffSwitchLabelsOf to use _of directly. This avoids the unnecessary null-coalescing fallback and makes the behavior consistent with other ...Of methods (like sizeOf, paddingOf, etc.) which throw if MediaQuery is missing.

  static bool onOffSwitchLabelsOf(BuildContext context) => _of(context, _MediaQueryAspect.onOffSwitchLabels).onOffSwitchLabels;
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

Comment on lines +2210 to +2211
static bool boldTextOf(BuildContext context) =>
_maybeOf(context, _MediaQueryAspect.boldText)?.boldText ?? false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since runApp always introduces a MediaQuery and there is no need to handle a missing MediaQuery (as established by the deprecation of the maybe... methods), we can simplify boldTextOf to use _of directly. This avoids the unnecessary null-coalescing fallback and makes the behavior consistent with other ...Of methods (like sizeOf, paddingOf, etc.) which throw if MediaQuery is missing.

  static bool boldTextOf(BuildContext context) => _of(context, _MediaQueryAspect.boldText).boldText;
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

Comment on lines +2242 to +2243
static bool supportsAnnounceOf(BuildContext context) =>
_maybeOf(context, _MediaQueryAspect.supportsAnnounce)?.supportsAnnounce ?? false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since runApp always introduces a MediaQuery and there is no need to handle a missing MediaQuery (as established by the deprecation of the maybe... methods), we can simplify supportsAnnounceOf to use _of directly. This avoids the unnecessary null-coalescing fallback and makes the behavior consistent with other ...Of methods (like sizeOf, paddingOf, etc.) which throw if MediaQuery is missing.

  static bool supportsAnnounceOf(BuildContext context) => _of(context, _MediaQueryAspect.supportsAnnounce).supportsAnnounce;
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

@nate-thegrate

nate-thegrate commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

I took care not to change any Material or Cupertino package files, but it looks like editing packages/flutter/test_fixes/material/material.dart.expect caused the code freeze check to fail.

material_expect

Edit: I decided to revert this change, hopefully packages/flutter/test_fixes/material/ isn't part of the framework's CI/CD pipeline.

@nate-thegrate

nate-thegrate commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Even though runApp() guarantees an ancestor MediaQuery, it's technically possible to build a widget without one, e.g. by using wrapWithView: false in a widget test.

testWidgets('RichText defaults to 1.0 devicePixelRatio when no View or MediaQuery is present', (
WidgetTester tester,
) async {
await tester.pumpWidget(
RawView(
view: tester.view,
child: LookupBoundary(
child: Directionality(
textDirection: TextDirection.ltr,
child: RichText(text: const TextSpan(text: 'Hello')),
),
),
),
wrapWithView: false,
);
final RenderParagraph paragraph = tester.renderObject(find.byType(RichText));
expect(paragraph.devicePixelRatio, 1.0);
});

Implementing Gemini's suggestions would break this test, so we'd need to figure out how to fix it. (Given how many different widgets already break without a MediaQuery, perhaps we ought to turn RawView into a private class to prevent developers from self-sabotaging.)
IMO we should leave this PR as-is and save any further changes for a later discussion.

@github-actions github-actions Bot removed p: material_ui material_ui package in flutter/packages f: cupertino labels Oct 12, 2026
@Renzo-Olivares Renzo-Olivares removed the a: text input Entering text in a text field or keyboard related problems label Oct 29, 2026
@nate-thegrate

Copy link
Copy Markdown
Contributor Author

Closing this PR based on the conversation in #183701.

@nate-thegrate
nate-thegrate deleted the deprecate-MediaQuery-maybeOf branch November 10, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: tech-debt Technical debt, code quality, testing, etc. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: shrink the MediaQuery API surface

2 participants