Skip to content

ShowDialog is using the wrong context #28505

Closed

Description

@MMonrad

Hello there,

The issues I addressed is in this line: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/dialog.dart#L705

When the showDialog pageBuilder is trying to get the theme it is using the context from showDialog and not the context from the pageBuilder. The issue is that the context from showDialog can be invalid ex. when the application waked from a suspended state.

My suggestion to fix it, is to change:

final ThemeData theme = Theme.of(context, shadowThemeOnly: true);

to

final ThemeData theme = Theme.of(buildContext, shadowThemeOnly: true);

Steps to Reproduce

  1. Call Show dialog, with a textfield as child
  2. Suspend the entire application
  3. Wake up the application
  4. Go to landscape mode and see the application will crash

Logs

Activity

  1. zoechi commented on Feb 26, 2019

    @zoechi
    Contributor

    Please add the output of flutter doctor -v.

    To investigate we would need a minimal runnable reproduction
    as a single file so that we can just copy your code into lib/main.dart of a new project and run to reproduce.

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 26, 2019
  3. MMonrad commented on Feb 26, 2019

    @MMonrad
    Author

    Here is some code where the issue is:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
      int _changeState = 0;
    
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        if (state == AppLifecycleState.resumed) {
          setState(() {
            _changeState = _changeState + 1;
          });
        }
      }
    
      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addObserver(this);
      }
    
      @override
      void dispose() {
        WidgetsBinding.instance.removeObserver(this);
        super.dispose();
      }
    
      Widget firstDraw() {
        return MyWidget(    );
      }
    
      Widget secondDraw() {
        return FloatingActionButton(
          onPressed: () {
            showDialog(
                context: context,
                builder: (buildContext) {
                  return AlertDialog(
                    content: TextField(),
                  );
                });
          },
          tooltip: 'Show',
          child: Icon(Icons.add),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text(widget.title),
            ),
            body: _changeState == 0 ? firstDraw() : secondDraw());
      }
    }
    
    class MyWidget extends StatefulWidget {
      @override
      _MyWidgetState createState() => _MyWidgetState();
    }
    
    class _MyWidgetState extends State<MyWidget> {
      @override
      Widget build(BuildContext context) {
        return FloatingActionButton(
            backgroundColor: Colors.amber,
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (buildContext) {
                    return AlertDialog(
                      content: TextField(),
                    );
                  });
            },
            tooltip: 'Show',
            child: Icon(Icons.add),
          );
      }
    }
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 26, 2019
  5. MMonrad commented on Feb 26, 2019

    @MMonrad
    Author

    And the flutter doctor output:

    [√] Flutter (Channel stable, v1.0.0, on Microsoft Windows [Version 10.0.17763.316], locale da-DK)
        • Flutter version 1.0.0 at C:\flutter
        • Framework revision 5391447fae (3 months ago), 2018-11-29 19:41:26 -0800
        • Engine revision 7375a0f414
        • Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297)
    
    [√] Android toolchain - develop for Android devices (Android SDK 28.0.3)
        • Android SDK at C:\Users\mads\AppData\Local\Android\sdk
        • Android NDK location not configured (optional; useful for native profiling support)
        • Platform android-28, build-tools 28.0.3
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
        • All Android licenses accepted.
    
    [√] Android Studio (version 3.3)
        • Android Studio at C:\Program Files\Android\Android Studio
        X Flutter plugin not installed; this adds Flutter specific functionality.
        X Dart plugin not installed; this adds Dart specific functionality.
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    
    [√] VS Code (version 1.31.1)
        • VS Code at C:\Users\mads\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 2.23.1
    
    [!] Connected device
        • Device ce061606d8c0352e03 is offline.
    
    ! Doctor found issues in 1 category.
    
  6. zoechi commented on Feb 26, 2019

    @zoechi
    Contributor

    Could you please try with dev channel

    flutter channel dev
    flutter doctor
    

    I was not able to reproduce with

    [✓] Flutter (Channel master, v1.2.3-pre.47, on Mac OS X 10.14.3 18D42, locale en-AT)
    
    [✓] Connected device (1 available)
        • Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)
    
  7. added
    c: crashStack traces logged to the console
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Feb 26, 2019
  8. MMonrad commented on Feb 26, 2019

    @MMonrad
    Author

    I am still able to reproduce the error on dev channel.

    I/flutter ( 8178): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
    I/flutter ( 8178): The following assertion was thrown building Builder(dirty):
    I/flutter ( 8178): Looking up a deactivated widget's ancestor is unsafe.
    I/flutter ( 8178): At this point the state of the widget's element tree is no longer stable. To safely refer to a
    I/flutter ( 8178): widget's ancestor in its dispose() method, save a reference to the ancestor by calling
    I/flutter ( 8178): inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.
    I/flutter ( 8178): 
    I/flutter ( 8178): When the exception was thrown, this was the stack:
    I/flutter ( 8178): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> 
    I/flutter ( 8178): #1      Element._debugCheckStateIsActiveForAncestorLookup 
    I/flutter ( 8178): #2      Element.inheritFromWidgetOfExactType 
    I/flutter ( 8178): #3      Theme.of 
    I/flutter ( 8178): #4      showDialog.<anonymous closure> 
    I/flutter ( 8178): #5      _DialogRoute.buildPage 
    I/flutter ( 8178): #6      _ModalScopeState.build.<anonymous closure> 
    I/flutter ( 8178): #7      Builder.build 
    I/flutter ( 8178): #8      StatelessElement.build 
    I/flutter ( 8178): #9      ComponentElement.performRebuild 
    I/flutter ( 8178): #10     Element.rebuild 
    I/flutter ( 8178): #11     StatelessElement.update 
    I/flutter ( 8178): #12     Element.updateChild 
    I/flutter ( 8178): #13     SingleChildRenderObjectElement.update 
    I/flutter ( 8178): #14     Element.updateChild 
    I/flutter ( 8178): #15     SingleChildRenderObjectElement.update 
    I/flutter ( 8178): #16     Element.updateChild 
    I/flutter ( 8178): #17     SingleChildRenderObjectElement.update 
    I/flutter ( 8178): #18     Element.updateChild 
    I/flutter ( 8178): #19     ComponentElement.performRebuild 
    I/flutter ( 8178): #20     Element.rebuild 
    I/flutter ( 8178): #21     StatefulElement.update 
    I/flutter ( 8178): #22     Element.updateChild 
    I/flutter ( 8178): #23     SingleChildRenderObjectElement.update 
    I/flutter ( 8178): #24     Element.updateChild 
    I/flutter ( 8178): #25     ComponentElement.performRebuild 
    I/flutter ( 8178): #26     Element.rebuild 
    I/flutter ( 8178): #27     ProxyElement.update 
    I/flutter ( 8178): #28     Element.updateChild 
    I/flutter ( 8178): #29     SingleChildRenderObjectElement.update 
    I/flutter ( 8178): #30     Element.updateChild 
    I/flutter ( 8178): #31     ComponentElement.performRebuild 
    I/flutter ( 8178): #32     Element.rebuild 
    I/flutter ( 8178): #33     StatefulElement.update 
    I/flutter ( 8178): #34     Element.updateChild 
    I/flutter ( 8178): #35     ComponentElement.performRebuild 
    I/flutter ( 8178): #36     Element.rebuild 
    I/flutter ( 8178): #37     StatelessElement.update 
    I/flutter ( 8178): #38     Element.updateChild 
    I/flutter ( 8178): #39     SingleChildRenderObjectElement.update 
    I/flutter ( 8178): #40     Element.updateChild 
    I/flutter ( 8178): #41     ComponentElement.performRebuild 
    I/flutter ( 8178): #42     Element.rebuild 
    I/flutter ( 8178): #43     ProxyElement.update 
    I/flutter ( 8178): #44     Element.updateChild 
    I/flutter ( 8178): #45     ComponentElement.performRebuild 
    I/flutter ( 8178): #46     Element.rebuild 
    I/flutter ( 8178): #47     BuildOwner.buildScope 
    I/flutter ( 8178): #48     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame 
    I/flutter ( 8178): #49     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback 
    I/flutter ( 8178): #50     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback 
    I/flutter ( 8178): #51     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame 
    I/flutter ( 8178): #52     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame 
    I/flutter ( 8178): #56     _invoke (dart:ui/hooks.dart:209:10)
    I/flutter ( 8178): #57     _drawFrame (dart:ui/hooks.dart:168:3)
    I/flutter ( 8178): (elided 3 frames from package dart:async)
    I/flutter ( 8178): ════════════════════════════════════════════════════════════════════════════════════════════════════
    
  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 26, 2019
  10. MMonrad commented on Feb 26, 2019

    @MMonrad
    Author

    Here is a screen record of it, it is still on the dev channel

    Screen Record.zip

  11. added this to the milestone on Feb 26, 2019
  12. github-actions commented on Aug 14, 2021

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

  13. locked as resolved and limited conversation to collaborators on Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

c: crashStack traces logged to the consoleframeworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packageswaiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions