Repository: flutter/flutter
File: packages/flutter/lib/src/widgets/overlay.dart (Dart)

Code before:
    _locationCache?._debugMarkLocationInvalid();
    _locationCache = null;
  }

  void hide() {
    assert(SchedulerBinding.instance.schedulerPhase != SchedulerPhase.persistentCallbacks);
    setState(() {
      _zOrderIndex = null;
    });
    _locationCache?._debugMarkLocationInvalid();
    _locationCache = null;
  }

  @override
  Widget build(BuildContext context) {
    final int? zOrderIndex = _zOrderIndex;
    if (zOrderIndex == null) {
      return _OverlayPortal(
        overlayLocation: null,
        overlayChild: null,
        child: Semantics(traversalParentIdentifier: this, child: widget.child),
      );
    }

    MediaQueryData data = MediaQuery.of(context);
    final OverlayState? overlay = Overlay.maybeOf(
      context,
      rootOverlay: widget.overlayLocation == OverlayChildLocation.rootOverlay,
    );
    if (overlay != null) {
      final MediaQueryData overlayData = MediaQuery.of(overlay.context);
      data = data.copyWith(
        padding: overlayData.padding,
        viewPadding: overlayData.viewPadding,
        viewInsets: overlayData.viewInsets,
      );
    }
    return _OverlayPortal(
      overlayLocation: _getLocation(zOrderIndex, widget.overlayLocation),
      overlayChild: _DeferredLayout(
        childIdentifier: this,
        child: MediaQuery(
          data: data,
          child: Builder(builder: widget.overlayChildBuilder),
        ),
      ),
      child: Semantics(traversalParentIdentifier: this, child: widget.child),
    );
  }
}


Review comment (suggestion):
You can get the right context for the Overlay from the `_getLocation` call instead of looking it up again using `Overlay.of`.

Code after:
    _locationCache?._debugMarkLocationInvalid();
    _locationCache = null;
  }

  void hide() {
    assert(SchedulerBinding.instance.schedulerPhase != SchedulerPhase.persistentCallbacks);
    setState(() {
      _zOrderIndex = null;
    });
    _locationCache?._debugMarkLocationInvalid();
    _locationCache = null;
  }

  @override
  Widget build(BuildContext context) {
    final int? zOrderIndex = _zOrderIndex;
    if (zOrderIndex == null) {
      return _OverlayPortal(
        overlayLocation: null,
        overlayChild: null,
        child: Semantics(traversalParentIdentifier: this, child: widget.child),
      );
    }

    final _OverlayEntryLocation overlayLocation = _getLocation(zOrderIndex, widget.overlayLocation);
    final MediaQueryData overlayData = MediaQuery.of(overlayLocation._childModel.context);
    final MediaQueryData data = MediaQuery.of(context).copyWith(
      padding: overlayData.padding,
      viewInsets: overlayData.viewInsets,
      viewPadding: overlayData.viewPadding,
    );
    return _OverlayPortal(
      overlayLocation: overlayLocation,
      overlayChild: _DeferredLayout(
        childIdentifier: this,
        child: MediaQuery(
          data: data,
          child: Builder(builder: widget.overlayChildBuilder),
        ),
      ),
      child: Semantics(traversalParentIdentifier: this, child: widget.child),
    );
  }
}

/// A location in an [Overlay].
///
/// An [_OverlayEntryLocation] determines the [Overlay] the associated
/// [OverlayPortal] should put its overlay child onto, as well as the overlay
/// child's paint order in relation to other contents painted on the [Overlay].
//