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
b8df71fc
Unverified
Commit
b8df71fc
authored
Sep 15, 2022
by
Michael Goderbauer
Committed by
GitHub
Sep 15, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add BuildContext.mounted (#111619)
parent
fb28a0d3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
framework.dart
packages/flutter/lib/src/widgets/framework.dart
+38
-0
build_context_test.dart
packages/flutter/test/widgets/build_context_test.dart
+63
-0
No files found.
packages/flutter/lib/src/widgets/framework.dart
View file @
b8df71fc
...
...
@@ -2081,6 +2081,29 @@ typedef ElementVisitor = void Function(Element element);
///
/// {@youtube 560 315 https://www.youtube.com/watch?v=rIaaH87z1-g}
///
/// Avoid storing instances of [BuildContext]s because they may become invalid
/// if the widget they are associated with is unmounted from the widget tree.
/// {@template flutter.widgets.BuildContext.asynchronous_gap}
/// If a [BuildContext] is used across an asynchronous gap (i.e. after performing
/// an asynchronous operation), consider checking [mounted] to determine whether
/// the context is still valid before interacting with it:
///
/// ```dart
/// @override
/// Widget build(BuildContext context) {
/// return OutlinedButton(
/// onPressed: () async {
/// await Future<void>.delayed(const Duration(seconds: 1));
/// if (context.mounted) {
/// Navigator.of(context).pop();
/// }
/// },
/// child: const Text('Delayed pop'),
/// );
/// }
/// ```
/// {@endtemplate}
///
/// [BuildContext] objects are actually [Element] objects. The [BuildContext]
/// interface is used to discourage direct manipulation of [Element] objects.
abstract
class
BuildContext
{
...
...
@@ -2091,6 +2114,18 @@ abstract class BuildContext {
/// managing the rendering pipeline for this context.
BuildOwner
?
get
owner
;
/// Whether the [Widget] this context is associated with is currently
/// mounted in the widget tree.
///
/// Accessing the properties of the [BuildContext] or calling any methods on
/// it is only valid while mounted is true. If mounted is false, assertions
/// will trigger.
///
/// Once unmounted, a given [BuildContext] will never become mounted again.
///
/// {@macro flutter.widgets.BuildContext.asynchronous_gap}
bool
get
mounted
;
/// Whether the [widget] is currently updating the widget or render tree.
///
/// For [StatefulWidget]s and [StatelessWidget]s this flag is true while
...
...
@@ -3271,6 +3306,9 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
Widget
get
widget
=>
_widget
!;
Widget
?
_widget
;
@override
bool
get
mounted
=>
_widget
!=
null
;
/// Returns true if the Element is defunct.
///
/// This getter always returns false in profile and release builds.
...
...
packages/flutter/test/widgets/build_context_test.dart
0 → 100644
View file @
b8df71fc
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter/widgets.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
void
main
(
)
{
testWidgets
(
'StatefulWidget BuildContext.mounted'
,
(
WidgetTester
tester
)
async
{
late
BuildContext
capturedContext
;
await
tester
.
pumpWidget
(
TestStatefulWidget
(
onBuild:
(
BuildContext
context
)
{
capturedContext
=
context
;
}
));
expect
(
capturedContext
.
mounted
,
isTrue
);
await
tester
.
pumpWidget
(
Container
());
expect
(
capturedContext
.
mounted
,
isFalse
);
});
testWidgets
(
'StatelessWidget BuildContext.mounted'
,
(
WidgetTester
tester
)
async
{
late
BuildContext
capturedContext
;
await
tester
.
pumpWidget
(
TestStatelessWidget
(
onBuild:
(
BuildContext
context
)
{
capturedContext
=
context
;
}
));
expect
(
capturedContext
.
mounted
,
isTrue
);
await
tester
.
pumpWidget
(
Container
());
expect
(
capturedContext
.
mounted
,
isFalse
);
});
}
typedef
BuildCallback
=
void
Function
(
BuildContext
);
class
TestStatelessWidget
extends
StatelessWidget
{
const
TestStatelessWidget
({
super
.
key
,
required
this
.
onBuild
});
final
BuildCallback
onBuild
;
@override
Widget
build
(
BuildContext
context
)
{
onBuild
(
context
);
return
Container
();
}
}
class
TestStatefulWidget
extends
StatefulWidget
{
const
TestStatefulWidget
({
super
.
key
,
required
this
.
onBuild
});
final
BuildCallback
onBuild
;
@override
State
<
TestStatefulWidget
>
createState
()
=>
_TestStatefulWidgetState
();
}
class
_TestStatefulWidgetState
extends
State
<
TestStatefulWidget
>
{
@override
Widget
build
(
BuildContext
context
)
{
widget
.
onBuild
(
context
);
return
Container
();
}
}
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