Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
f614c597
Unverified
Commit
f614c597
authored
Jul 08, 2022
by
Bruno Leroux
Committed by
GitHub
Jul 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix showDialog throws cryptic message when context is not active (#107323)
parent
c58dca2a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
0 deletions
+66
-0
dialog.dart
packages/flutter/lib/src/material/dialog.dart
+18
-0
framework.dart
packages/flutter/lib/src/widgets/framework.dart
+13
-0
dialog_test.dart
packages/flutter/test/material/dialog_test.dart
+35
-0
No files found.
packages/flutter/lib/src/material/dialog.dart
View file @
f614c597
...
...
@@ -1153,6 +1153,7 @@ Future<T?> showDialog<T>({
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<T?> showDialog<T>({
));
}
bool
_debugIsActive
(
BuildContext
context
)
{
if
(
context
is
Element
&&
!
context
.
debugIsActive
)
{
throw
FlutterError
.
fromParts
(<
DiagnosticsNode
>[
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).
...
...
packages/flutter/lib/src/widgets/framework.dart
View file @
f614c597
...
...
@@ -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
;
...
...
packages/flutter/test/material/dialog_test.dart
View file @
f614c597
...
...
@@ -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
<
void
>(
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
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment