Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling WidgetsBinding.attachRootWidget throws an Exception #121180

Closed
fertrig opened this issue Feb 22, 2023 · 4 comments
Closed

Calling WidgetsBinding.attachRootWidget throws an Exception #121180

fertrig opened this issue Feb 22, 2023 · 4 comments
Labels
a: error message Error messages from the Flutter framework c: crash Stack traces logged to the console c: regression It was better in the past than it is now found in release: 3.8 Found to occur in 3.8 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on r: solved Issue is closed as solved

Comments

@fertrig
Copy link

fertrig commented Feb 22, 2023

Using latest flutter beta (Flutter 3.8.0-10.1.pre) throws an exception when calling WidgetsBinding.attachRootWidget.

Steps to reproduce

  1. Using macos and Flutter 3.8.0-10.1.pre from the beta channel
  2. Run flutter create beta_crash
  3. Replace contents of main.dart with:
import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  var binding = WidgetsFlutterBinding.ensureInitialized();

  Timer.run(() {
    binding.attachRootWidget(MyApp());
  });
  binding.scheduleWarmUpFrame();
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Text('BAR'));
  }
}
  1. Run flutter run -d macos
  2. When the application launches you get this error
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Builder:
No MediaQuery widget ancestor found.
ScaffoldMessenger widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was:
  ScaffoldMessenger
The ownership chain for the affected widget is: "ScaffoldMessenger ← Builder ← DefaultTextStyle ←
  CustomPaint ← Banner ← CheckedModeBanner ← Title ← Directionality ←
  _LocalizationsScope-[GlobalKey#0a912] ← Semantics ← ⋯"
No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those
widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above
those widgets.

The relevant error-causing widget was:
  MaterialApp MaterialApp:file:///Users/fertrig/development/scratch/pop/lib/main.dart:20:12

When the exception was thrown, this was the stack:
#0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:297:7)
#1      debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:312:4)
#2      MediaQuery._of (package:flutter/src/widgets/media_query.dart:1017:12)
#3      MediaQuery.accessibleNavigationOf (package:flutter/src/widgets/media_query.dart:1208:63)
#4      ScaffoldMessengerState.didChangeDependencies (package:flutter/src/material/scaffold.dart:192:50)
#5      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5103:11)
#6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4928:5)
...     Normal element mounting (315 frames)
#321    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3940:16)
#322    Element.updateChild (package:flutter/src/widgets/framework.dart:3677:18)
#323    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1196:16)
#324    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1165:5)
#325    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1112:18)
#326    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2689:19)
#327    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)
#328    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7)
#329    main.<anonymous closure> (package:pop/main.dart:10:13)
#333    _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)
(elided 3 frames from class _Timer and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════

If you create a different sample project using Flutter stable 3.7.3 or prior, and use the same code above, the application loads successfully.

The error should be reproducible in other platforms and targets.

I suspect the error is related to this commit. Is this a breaking change?

@huycozy huycozy added the in triage Presently being triaged by the triage team label Feb 22, 2023
@huycozy

This comment was marked as outdated.

@huycozy huycozy closed this as completed Feb 22, 2023
@huycozy huycozy added r: fixed Issue is closed as already fixed in a newer version and removed in triage Presently being triaged by the triage team labels Feb 22, 2023
@huycozy
Copy link
Member

huycozy commented Feb 22, 2023

@fertrig It's weird as the error appears now. Sorry for the quick conclusion. I reopen this for further investigation.

This issue is reproducible on the latest master and beta channels whilst it works as expected on the latest stable(3.7.4).

stable 3.7.4 beta 3.8.0-10.1.pre master 3.8.0-14.0.pre.25

✅: No Issue ❌: Issue reproduced

flutter doctor -v (stable, beta, master)
[✓] Flutter (Channel stable, 3.7.4, on macOS 13.0.1 22A400 darwin-x64, locale en-EE)
    • Flutter version 3.7.4 on channel stable at /Users/huynq/Documents/GitHub/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b4bce91dd0 (27 hours ago), 2023-02-21 09:50:50 +0800
    • Engine revision 248290d6d5
    • Dart version 2.19.2
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Users/huynq/Library/Java/JavaVirtualMachines/corretto-1.8.0_302/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment Corretto-8.302.08.1 (build 1.8.0_302-b08)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version 2022.1)
    • 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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
    • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
    • 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

[✓] VS Code (version 1.75.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.58.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 110.0.5481.100

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
[!] Flutter (Channel master, 3.8.0-14.0.pre.25, on macOS 13.0.1 22A400 darwin-x64, locale en-EE)
    • Flutter version 3.8.0-14.0.pre.25 on channel master at /Users/huynq/Documents/GitHub/flutter_master
    ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4c25587b71 (5 hours ago), 2023-02-21 16:10:25 -0800
    • Engine revision 3aa112c75c
    • Dart version 3.0.0 (build 3.0.0-261.0.dev)
    • DevTools version 2.21.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 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • 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.15+0-b2043.56-8887301)

[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
    • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
    • 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

[✓] VS Code (version 1.75.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.58.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 110.0.5481.100

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
[!] Flutter (Channel beta, 3.8.0-10.1.pre, on macOS 13.0.1 22A400 darwin-x64, locale en-EE)
    • Flutter version 3.8.0-10.1.pre on channel beta at /Users/huynq/Documents/GitHub/flutter_beta
    ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_beta. Consider adding /Users/huynq/Documents/GitHub/flutter_beta/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_beta. Consider adding /Users/huynq/Documents/GitHub/flutter_beta/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 1a0a03a41d (5 days ago), 2023-02-16 22:03:58 -0600
    • Engine revision 639e313f99
    • Dart version 3.0.0 (build 3.0.0-218.1.beta)
    • DevTools version 2.21.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 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • 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.15+0-b2043.56-8887301)

[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
    • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
    • 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

[✓] VS Code (version 1.75.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.58.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 110.0.5481.100

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

@huycozy huycozy reopened this Feb 22, 2023
@huycozy huycozy added framework flutter/packages/flutter repository. See also f: labels. a: error message Error messages from the Flutter framework has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 3.8 Found to occur in 3.8 c: regression It was better in the past than it is now c: crash Stack traces logged to the console and removed r: fixed Issue is closed as already fixed in a newer version labels Feb 22, 2023
@bleroux
Copy link
Contributor

bleroux commented Feb 22, 2023

Since #119377 (part of the multi-window effort), MaterialApp no more insert a MediaQueryinto the tree.

runApp() was updated accordingly (see #116924).

The provided code sample can be updated in a similar way (adding a call to WidgetsFlutterBinding.wrapWithDefaultView which will take care of MediaQuery creation):

Updated code sample
import 'package:flutter/material.dart';

void main() {
  var binding = WidgetsFlutterBinding.ensureInitialized();

  Timer.run(() {
    binding.attachRootWidget(
      binding.wrapWithDefaultView(const MyApp()),
    );
  });
  binding.scheduleWarmUpFrame();
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: Text('BAR'));
  }
}

@github-actions
Copy link

github-actions bot commented Mar 8, 2023

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: error message Error messages from the Flutter framework c: crash Stack traces logged to the console c: regression It was better in the past than it is now found in release: 3.8 Found to occur in 3.8 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on r: solved Issue is closed as solved
Projects
Status: Issue closed with comment
Development

No branches or pull requests

3 participants