Repository: flutter
Issue: OverlayEntry doesn't show up in release mode Error: Unhandled Exception: Null check operator used on a null value
Labels: c: crash,framework,a: release,has reproducible steps,a: null-safety,P2,found in release: 3.0,found in release: 3.1,team-framework,triaged-framework

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

OverlayEntry does not show up in release mode but works fine in debug mode

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. Compare the result in debug and release mode

**Expected results:** <!-- what did you want to see? --> The overlay should be visible in release mode

**Actual results:** <!-- what did you see? --> The overlay is not visible in release mode but only in debug mode

This is probably a regression introduced in flutter 3

release  | debug
:-: | :-:
<video src='https://user-images.githubusercontent.com/31410839/176449039-fbfa87f7-26b6-4515-b51f-57f65047bdd1.mov' width=180/> | <video src='https://user-images.githubusercontent.com/31410839/176454783-d2b31948-bd20-4c23-b348-fd3838530a8e.mov' width=180/> 



You will notice this exception when you 
- run in release mode
-  focus in text field
-  enter some text and try tapping multiple times if you don't see the exception

<details>
<summary>error logs (release only)</summary>

```dart
mahesh@Maheshs-MacBook-Air-M1 sample % flutter run --release
Launching lib/main.dart on Redmi K20 Pro in release mode...
Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01
Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01
Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01
Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01
Running Gradle task 'assembleRelease'...                            3.5s
✓  Built build/app/outputs/flutter-apk/app-release.apk (8.0MB).

Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
E/flutter ( 7267): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Null check operator used on a null value
E/flutter ( 7267): #0      RenderBox.hitTest (package:flutter/src/rendering/box.dart:2462)
E/flutter ( 7267): #1      _RenderTheatre.hitTestChildren.<anonymous closure> (package:flutter/src/widgets/overlay.dart:771)
E/flutter ( 7267): #2      BoxHitTestResult.addWithPaintOffset (package:flutter/src/rendering/box.dart:785)
E/flutter ( 7267): #3      _RenderTheatre.hitTestChildren (package:flutter/src/widgets/overlay.dart:766)
E/flutter ( 7267): #4      RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #5      RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #6      RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #7      RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #8      RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #9      RenderAbsorbPointer.hitTest (package:flutter/src/rendering/proxy_box.dart:3566)
E/flutter ( 7267): #10     RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #11     RenderProxyBoxWithHitTestBehavior.hitTest (package:flutter/src/rendering/proxy_box.dart:178)
E/flutter ( 7267): #12     RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #13     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #14     RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #15     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #16     RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #17     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #18     RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:131)
E/flutter ( 7267): #19     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2463)
E/flutter ( 7267): #20     RenderView.hitTest (package:flutter/src/rendering/view.dart:185)
E/flutter ( 7267): #21     RendererBinding.hitTest (package:flutter/src/rendering/binding.dart:538)
E/flutter ( 7267): #22     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:352)
E/flutter ( 7267): #23     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:344)
E/flutter ( 7267): #24     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:302)
E/flutter ( 7267): #25     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:285)
E/flutter ( 7267): #26     _rootRunUnary (dart:async/zone.dart:1442)
E/flutter ( 7267): #27     _CustomZone.runUnary (dart:async/zone.dart:1335)
E/flutter ( 7267): #28     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244)
E/flutter ( 7267): #29     _invoke1 (dart:ui/hooks.dart:170)
E/flutter ( 7267): #30     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331)
E/flutter ( 7267): #31     _dispatchPointerDataPacket (dart:ui/hooks.dart:94)
E/flutter ( 7267): 
```
</details> 





<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final ScrollController _scrollController = ScrollController();

  Widget _list() {
    return Container(
      height: 5 * 40,
      child: Scrollbar(
          controller: _scrollController,
          thumbVisibility: true,
          child: ListView.builder(
              controller: _scrollController,
              padding: EdgeInsets.zero,
              itemCount: 20,
              itemBuilder: (context, index) => ListTile(
                    title: Text('item $index'),
                  ))),
    );
  }

  final LayerLink _layerLink = LayerLink();

  OverlayEntry _createOverlay() {
    final renderBox = context.findRenderObject() as RenderBox;
    final size = renderBox.size;
    final offset = renderBox.localToGlobal(Offset.zero);
    return OverlayEntry(
        builder: (context) => Positioned(
              left: offset.dx,
              width: size.width,
              child: CompositedTransformFollower(
                  offset: Offset(0, 50),
                  link: _layerLink,
                  child: Material(color: Colors.red, child: _list())),
            ));
  }

  late OverlayEntry _overlayEntry;

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _overlayEntry = _createOverlay();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CompositedTransformTarget(
              link: _layerLink,
              child: TextFormField(
                decoration: InputDecoration(
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Colors.black.withOpacity(0.8),
                    ),
                  ),
                  border: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.red),
                  ),
                ),
                onTap: () {
                  Overlay.of(context)!.insert(_overlayEntry);
                },
                onChanged: (query) {},
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

```

</details>

<details>
<summary>flutter doctor -v (mac)</summary>

```
[✓] Flutter (Channel stable, 3.0.3, on macOS 12.4 21F79 darwin-arm, locale en-IN)
    • Flutter version 3.0.3 at /Users/mahesh/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 676cefaaff (5 days ago), 2022-06-22 11:34:49 -0700
    • Engine revision ffe7b86a1e
    • Dart version 2.17.5
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-32, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.67.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.43.20220621

[✓] Connected device (3 available)
    • sdk gphone arm64 (mobile) • emulator-5554 • android-arm64  • Android 11 (API 30) (emulator)
    • macOS (desktop)           • macos         • darwin-arm64   • macOS 12.4 21F79 darwin-arm
    • Chrome (web)              • chrome        • web-javascript • Google Chrome 103.0.5060.53

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```
```
[✓] Flutter (Channel master, 3.1.0-0.0.pre.1376, on macOS 12.4 21F79 darwin-arm, locale en-IN)
    • Flutter version 3.1.0-0.0.pre.1376 on channel master at /Users/mahesh/Documents/flutter_master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 3598f20002 (9 hours ago), 2022-06-26 18:19:04 -0400
    • Engine revision ee56813c14
    • Dart version 2.18.0 (build 2.18.0-228.0.dev)
    • DevTools version 2.14.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-32, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.67.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.43.20220621

[✓] Connected device (3 available)
    • sdk gphone arm64 (mobile) • emulator-5554 • android-arm64  • Android 11 (API 30) (emulator)
    • macOS (desktop)           • macos         • darwin-arm64   • macOS 12.4 21F79 darwin-arm
    • Chrome (web)              • chrome        • web-javascript • Google Chrome 103.0.5060.53

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>

<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->