From f614c597022fd1fafca698f2fd824a707223c84e Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Fri, 8 Jul 2022 23:40:08 +0200 Subject: [PATCH] Fix showDialog throws cryptic message when context is not active (#107323) --- packages/flutter/lib/src/material/dialog.dart | 18 ++++++++++ .../flutter/lib/src/widgets/framework.dart | 13 +++++++ .../flutter/test/material/dialog_test.dart | 35 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 0f440ade76..26f40169db 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -1153,6 +1153,7 @@ Future showDialog({ assert(barrierDismissible != null); assert(useSafeArea != null); assert(useRootNavigator != null); + assert(_debugIsActive(context)); assert(debugCheckHasMaterialLocalizations(context)); final CapturedThemes themes = InheritedTheme.capture( @@ -1176,6 +1177,23 @@ Future showDialog({ )); } +bool _debugIsActive(BuildContext context) { + if (context is Element && !context.debugIsActive) { + throw FlutterError.fromParts([ + ErrorSummary('This BuildContext is no longer valid.'), + ErrorDescription( + 'The showDialog function context parameter is a BuildContext that is no longer valid.' + ), + ErrorHint( + 'This can commonly occur when the showDialog function is called after awaiting a Future. ' + 'In this situation the BuildContext might refer to a widget that has already been disposed during the await. ' + 'Consider using a parent context instead.', + ), + ]); + } + return true; +} + /// A dialog route with Material entrance and exit animations, /// modal barrier color, and modal barrier behavior (dialog is dismissible /// with a tap on the barrier). diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index cfcaf612dd..1e0e6ee0d7 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -3264,6 +3264,19 @@ abstract class Element extends DiagnosticableTree implements BuildContext { return isDefunct; } + /// Returns true if the Element is active. + /// + /// This getter always returns false in profile and release builds. + /// See the lifecycle documentation for [Element] for additional information. + bool get debugIsActive { + bool isActive = false; + assert(() { + isActive = _lifecycleState == _ElementLifecycle.active; + return true; + }()); + return isActive; + } + /// The object that manages the lifecycle of this element. @override BuildOwner? get owner => _owner; diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 051160915b..f71677cf29 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -2122,6 +2122,41 @@ void main() { expect(nestedObserver.dialogCount, 1); }); + testWidgets('showDialog throws a friendly user message when context is not active', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/12467 + await tester.pumpWidget( + const MaterialApp( + home: Center(child: Text('Test')), + ), + ); + final BuildContext context = tester.element(find.text('Test')); + + await tester.pumpWidget( + const MaterialApp( + home: Center(), + ), + ); + + Object? error; + try { + showDialog( + context: context, + builder: (BuildContext innerContext) { + return const AlertDialog(title: Text('Title')); + }, + ); + } catch(exception) { + error = exception; + } + + expect(error, isNotNull); + expect(error, isFlutterError); + if (error is FlutterError) { + final ErrorSummary summary = error.diagnostics.first as ErrorSummary; + expect(summary.toString(), 'This BuildContext is no longer valid.'); + } + }); + group('showDialog avoids overlapping display features', () { testWidgets('positioning with anchorPoint', (WidgetTester tester) async { await tester.pumpWidget(