Skip to content

Regression “Null check operator used on a null value” thrown by LayoutBuilder #168936

Closed

Description

@b3nni97

Steps to reproduce

  1. Run the minimal reproducible sample on the current master channel.
  2. Wait ~1 – 2 s – the app crashes with the error
    "Null check operator used on a null value" coming from LayoutBuilder.

Expected results

The sample should run without errors.

Actual results

"Null check operator used on a null value” exceptions are thrown permanently.

Regression details:
First bad commit: merge of PR #154681 – “Make sure LayoutBuilder rebuild in an inactive route” (28 Mar 2025).
git bisect confirms that the crash is introduced by this PR.
@LongCatIsLooong

Code sample

Code sample

I rebuilt the key parts of our app’s complex widget hierarchy in this sample; the structure is deliberate, because simpler layouts don’t trigger the bug.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    CupertinoApp(
      home: CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          middle: Text('LayoutBuilder issue'),
        ),
        child: TestWidget(),
      ),
    ),
  );
}

class TestWidget extends StatefulWidget {
  @override
  State<TestWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  late final AnimationController _controller;
  late final Animation<double> _height;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    );

    _height = Tween<double>(
      begin: 0.1,
      end: 0.5,
    ).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut));

    _controller.repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return AnimatedBuilder(
      animation: _height,
      builder: (context, child) {
        return FractionallySizedBox(
          heightFactor: _height.value,
          child: CustomScrollView(
            slivers: [
              SliverList(
                delegate: SliverChildListDelegate([TestChild(), TestChild()]),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class TestChild extends StatefulWidget {
  @override
  State<TestChild> createState() => _TestChildState();
}

class _TestChildState extends State<TestChild>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Column(
      children: [
        Container(
          height: 500,
          child: LayoutBuilder(
            builder: (context, constraints) {
              return Container(color: Colors.red, width: double.infinity);
            },
          ),
        ),
      ],
    );
  }
}

Screenshots or Video

Logs

Logs
════════ Exception caught by rendering library ═════════════════════════════════
The following _TypeError was thrown during performLayout():
Null check operator used on a null value

The relevant error-causing widget was:
    LayoutBuilder LayoutBuilder:file:///Users/xxx/Desktop/layout_builder_issue/lib/main.dart:91:18

When the exception was thrown, this was the stack:
#0      RenderObject.layout (package:flutter/src/rendering/object.dart:2716:96)
object.dart:2716
#1      _RenderLayoutBuilder.performLayout (package:flutter/src/widgets/layout_builder.dart:440:14)
layout_builder.dart:440
#2      RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:2627:7)
object.dart:2627
#3      PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1145:18)
object.dart:1145
#4      PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1158:15)
object.dart:1158
#5      RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:629:23)
binding.dart:629
#6      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1231:13)
binding.dart:1231
#7      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:495:5)
binding.dart:495
#8      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1438:15)
binding.dart:1438
#9      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1351:9)
binding.dart:1351
#10     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1204:5)
binding.dart:1204
#11     _invoke (dart:ui/hooks.dart:331:13)
hooks.dart:331
#12     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:444:5)
platform_dispatcher.dart:444
#13     _drawFrame (dart:ui/hooks.dart:303:31)
hooks.dart:303

The following RenderObject was being processed when the exception was fired: _RenderLayoutBuilder#c6cb0 NEEDS-LAYOUT NEEDS-PAINT
    parentData: <none> (can use size)
    constraints: BoxConstraints(0.0<=w<=440.0, h=500.0)
    size: Size(440.0, 500.0)
    child: RenderConstrainedBox#52f91 NEEDS-PAINT
        parentData: offset=Offset(0.0, 0.0) (can use size)
        constraints: BoxConstraints(0.0<=w<=440.0, h=500.0)
        size: Size(440.0, 500.0)
        additionalConstraints: BoxConstraints(w=Infinity, 0.0<=h<=Infinity)
        child: _RenderColoredBox#6bdaa NEEDS-PAINT
            parentData: <none> (can use size)
            constraints: BoxConstraints(w=440.0, h=500.0)
            size: Size(440.0, 500.0)
            behavior: opaque
            child: RenderLimitedBox#6da64 NEEDS-PAINT
                parentData: <none> (can use size)
                constraints: BoxConstraints(w=440.0, h=500.0)
                size: Size(440.0, 500.0)
                maxWidth: 0.0
                maxHeight: 0.0
                child: RenderConstrainedBox#e6ea7 NEEDS-PAINT
                    parentData: <none> (can use size)
                    constraints: BoxConstraints(w=440.0, h=500.0)
                    size: Size(440.0, 500.0)
                    additionalConstraints: BoxConstraints(biggest)
RenderObject: _RenderLayoutBuilder#c6cb0 NEEDS-LAYOUT NEEDS-PAINT
    parentData: <none> (can use size)
    constraints: BoxConstraints(0.0<=w<=440.0, h=500.0)
    size: Size(440.0, 500.0)
    child: RenderConstrainedBox#52f91 NEEDS-PAINT
        parentData: offset=Offset(0.0, 0.0) (can use size)
        constraints: BoxConstraints(0.0<=w<=440.0, h=500.0)
        size: Size(440.0, 500.0)
        additionalConstraints: BoxConstraints(w=Infinity, 0.0<=h<=Infinity)
        child: _RenderColoredBox#6bdaa NEEDS-PAINT
            parentData: <none> (can use size)
            constraints: BoxConstraints(w=440.0, h=500.0)
            size: Size(440.0, 500.0)
            behavior: opaque
            child: RenderLimitedBox#6da64 NEEDS-PAINT
                parentData: <none> (can use size)
                constraints: BoxConstraints(w=440.0, h=500.0)
                size: Size(440.0, 500.0)
                maxWidth: 0.0
                maxHeight: 0.0
                child: RenderConstrainedBox#e6ea7 NEEDS-PAINT
                    parentData: <none> (can use size)
                    constraints: BoxConstraints(w=440.0, h=500.0)
                    size: Size(440.0, 500.0)
                    additionalConstraints: BoxConstraints(biggest)

Flutter Doctor output

Doctor output
Flutter 3.33.0-1.0.pre.78 • channel master • https://github.com/flutter/flutter.git
Framework • revision 773d076621 (21 hours ago) • 2025-05-14 15:59:14 -0700
Engine • revision 773d076621 (21 hours ago) • 2025-05-14 15:59:14 -0700
Tools • Dart 3.9.0 (build 3.9.0-117.0.dev) • DevTools 2.46.0

Activity

  1. danagbemava-nc commented on May 16, 2025

    @danagbemava-nc
    Member

    Reproducible using the code sample provided above. Can confirm that this works as expected on the stable branch but not the beta or master branches.

    flutter doctor -v
    [✓] Flutter (Channel stable, 3.29.3, on macOS 15.4 24E248 darwin-arm64, locale en-US) [1,096ms]
        • Flutter version 3.29.3 on channel stable at /Users/deanli/dev/stable
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ea121f8859 (5 weeks ago), 2025-04-11 19:10:07 +0000
        • Engine revision cf56914b32
        • Dart version 3.7.2
        • DevTools version 2.42.3
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [3.3s]
        • Android SDK at /Users/deanli/Library/Android/sdk
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents/jbr/Contents/Home/bin/java
          This JDK is specified in your Flutter configuration.
          To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.4) [1,319ms]
        • Xcode at /Applications/Xcode-16.4.0-Beta.app/Contents/Developer
        • Build 16F1t
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [9ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [9ms]
        • Android Studio at /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.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
        • android-studio-dir = /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    
    [✓] Android Studio (version 2024.2) [8ms]
        • Android Studio at /Users/deanli/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 21.0.5+-12932927-b750.29)
    
    [✓] VS Code (version 1.100.0) [7ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (5 available) [12.6s]
        • iPhone 16 Pro Max (wireless) (mobile) • 00008140-00194CC23A68801C • ios            • iOS 18.5 22F76
        • Nexus (wireless) (mobile)             • 00008020-001875E83A38002E • ios            • iOS 18.4.1 22E252
        • macOS (desktop)                       • macos                     • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Mac Designed for iPad (desktop)       • mac-designed-for-ipad     • darwin         • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)                          • chrome                    • web-javascript • Google Chrome 136.0.7103.114
        ! Error: Browsing on the local area network for Dean’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
        ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
    
    [✓] Network resources [1,589ms]
        • All expected network resources are available.
    
    • No issues found!
    
    [!] Flutter (Channel beta, 3.32.0-0.4.pre, on macOS 15.4 24E248 darwin-arm64, locale en-US) [433ms]
        • Flutter version 3.32.0-0.4.pre on channel beta at /Users/deanli/dev/beta
        ! Warning: `flutter` on your path resolves to /Users/deanli/dev/stable/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/deanli/dev/beta. Consider adding /Users/deanli/dev/beta/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/deanli/dev/stable/bin/dart, which is not inside your current Flutter SDK checkout at /Users/deanli/dev/beta. Consider adding /Users/deanli/dev/beta/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision dbc3660e84 (7 days ago), 2025-05-09 13:35:12 -0700
        • Engine revision 38e19d01dc
        • Dart version 3.8.0 (build 3.8.0-278.4.beta)
        • DevTools version 2.45.1
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [1,466ms]
        • Android SDK at /Users/deanli/Library/Android/sdk
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents/jbr/Contents/Home/bin/java
          This JDK is specified in your Flutter configuration.
          To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.4) [937ms]
        • Xcode at /Applications/Xcode-16.4.0-Beta.app/Contents/Developer
        • Build 16F1t
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [9ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [8ms]
        • Android Studio at /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.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
        • android-studio-dir = /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    
    [✓] Android Studio (version 2024.2) [7ms]
        • Android Studio at /Users/deanli/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 21.0.5+-12932927-b750.29)
    
    [✓] VS Code (version 1.100.0) [7ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (4 available) [6.1s]
        • iPhone 16 Pro Max (wireless) (mobile) • 00008140-00194CC23A68801C • ios            • iOS 18.5 22F76
        • Nexus (wireless) (mobile)             • 00008020-001875E83A38002E • ios            • iOS 18.4.1 22E252
        • macOS (desktop)                       • macos                     • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)                          • chrome                    • web-javascript • Google Chrome 136.0.7103.114
        ! Error: Browsing on the local area network for Dean’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
        ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
    
    [✓] Network resources [700ms]
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.33.0-1.0.pre.104, on macOS 15.4 24E248 darwin-arm64, locale en-US) [3.1s]
        • Flutter version 3.33.0-1.0.pre.104 on channel master at /Users/deanli/dev/master
        ! Warning: `flutter` on your path resolves to /Users/deanli/dev/stable/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/deanli/dev/master. Consider adding /Users/deanli/dev/master/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/deanli/dev/stable/bin/dart, which is not inside your current Flutter SDK checkout at /Users/deanli/dev/master. Consider adding /Users/deanli/dev/master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision c685293fc4 (8 hours ago), 2025-05-15 17:55:29 -0700
        • Engine revision c685293fc4
        • Dart version 3.9.0 (build 3.9.0-131.0.dev)
        • DevTools version 2.46.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [1,287ms]
        • Android SDK at /Users/deanli/Library/Android/sdk
        • Emulator version 35.5.9.0 (build_id 13248646) (CL:N/A)
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents/jbr/Contents/Home/bin/java
          This JDK is specified in your Flutter configuration.
          To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.4) [710ms]
        • Xcode at /Applications/Xcode-16.4.0-Beta.app/Contents/Developer
        • Build 16F1t
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [9ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [8ms]
        • Android Studio at /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.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
        • android-studio-dir = /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    
    [✓] Android Studio (version 2024.2) [7ms]
        • Android Studio at /Users/deanli/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 21.0.5+-12932927-b750.29)
    
    [✓] VS Code (version 1.100.0) [7ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (3 available) [6.0s]
        • iPhone 16 Pro Max (wireless) (mobile) • 00008140-00194CC23A68801C • ios            • iOS 18.5 22F76
        • macOS (desktop)                       • macos                     • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)                          • chrome                    • web-javascript • Google Chrome 136.0.7103.114
        ! Error: Browsing on the local area network for Dean’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
        ! Error: Browsing on the local area network for Nexus. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
        ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
    
    [✓] Network resources [743ms]
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
  2. added
    c: regressionIt was better in the past than it is now
    frameworkflutter/packages/flutter repository. See also f: labels.
    a: error messageError messages from the Flutter framework
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on May 16, 2025
  3. faisalansari0367 commented on May 21, 2025

    @faisalansari0367
    Contributor
    Image

    Even when the relayoutBoundary is null the else condition is being executed.
    Shall we add another check for it?

  4. added
    P2Important issues not at the top of the work list
    on May 21, 2025
  5. justinmc commented on May 21, 2025

    @justinmc
    Contributor

    @faisalansari0367 Can you link to that code?

    I'm also curious what PR broke this if someone can track it down.

  6. b3nni97 commented on May 22, 2025

    @b3nni97
    Author

    @justinmc I have already tracked this back to #154681

  7. faisalansari0367 commented on May 22, 2025

    @faisalansari0367
    Contributor

    @justinmc I have already tracked this back to #154681

    @b3nni97 Thanks for tracking it.
    Any idea what's the cause for it?

  8. OliverNarramore commented on May 23, 2025

    @OliverNarramore

    I'm also experiencing this issue on 3.32 (latest stable) it's a blocker for us.

  9. faisalansari0367 commented on May 28, 2025

    @faisalansari0367
    Contributor

    final RenderObject relayoutBoundary = isRelayoutBoundary ? this : parent!._relayoutBoundary ?? this;

    The above fix works but is this correct?

  10. JeromeGsq commented on Jun 6, 2025

    @JeromeGsq

    Any news or suggestions on this issue please?

  11. LongCatIsLooong commented on Jun 6, 2025

    @LongCatIsLooong
    Contributor

    Fixed in #169958

  12. added
    r: fixedIssue is closed as already fixed in a newer version
    on Jun 9, 2025
  13. blastervla commented on Jun 13, 2025

    @blastervla

    Hi. What's the ETA to get this fix to the stable channel? I see it merged in master but this is still broken in stable

  14. LongCatIsLooong commented on Jun 24, 2025

    @LongCatIsLooong
    Contributor

    Created a CP request. IIRC creating cherry picks doesn't require special permissions so you can follow the steps here in the future.

  15. github-actions commented on Jul 8, 2025

    @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.

  16. locked as resolved and limited conversation to collaborators on Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: error messageError messages from the Flutter frameworkc: regressionIt was better in the past than it is nowcustomer: googleVarious Google teamsfound in release: 3.32Found to occur in 3.32found in release: 3.33Found to occur in 3.33frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions