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
2a6f9ad6
Unverified
Commit
2a6f9ad6
authored
Aug 10, 2022
by
Paul Berry
Committed by
GitHub
Aug 10, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace NavigatorObserver._navigator with a static expando. (#109238)
parent
b1dab531
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
13 deletions
+71
-13
navigator.dart
packages/flutter/lib/src/widgets/navigator.dart
+23
-13
navigator_test.dart
packages/flutter/test/widgets/navigator_test.dart
+48
-0
No files found.
packages/flutter/lib/src/widgets/navigator.dart
View file @
2a6f9ad6
...
...
@@ -619,8 +619,18 @@ abstract class Page<T> extends RouteSettings {
/// An interface for observing the behavior of a [Navigator].
class
NavigatorObserver
{
/// The navigator that the observer is observing, if any.
NavigatorState
?
get
navigator
=>
_navigator
;
NavigatorState
?
_navigator
;
NavigatorState
?
get
navigator
=>
_navigators
[
this
];
/// Expando mapping instances of NavigatorObserver to their associated
/// NavigatorState (or `null`, if there is no associated NavigatorState). The
/// reason we don't simply use a private instance field of type
/// `NavigatorState?` is because as part of implementing
/// https://github.com/dart-lang/language/issues/2020, it will soon become a
/// runtime error to invoke a private member that is mocked in another
/// library. By using an expando rather than an instance field, we ensure
/// that a mocked NavigatorObserver can still properly keep track of its
/// associated NavigatorState.
static
final
Expando
<
NavigatorState
>
_navigators
=
Expando
<
NavigatorState
>();
/// The [Navigator] pushed `route`.
///
...
...
@@ -3236,7 +3246,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
}());
for
(
final
NavigatorObserver
observer
in
widget
.
observers
)
{
assert
(
observer
.
navigator
==
null
);
observer
.
_navigator
=
this
;
NavigatorObserver
.
_navigators
[
observer
]
=
this
;
}
_effectiveObservers
=
widget
.
observers
;
...
...
@@ -3359,12 +3369,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
ServicesBinding
.
instance
.
addPostFrameCallback
((
Duration
timestamp
)
{
// We only check if this navigator still owns the hero controller.
if
(
_heroControllerFromScope
==
newHeroController
)
{
final
bool
hasHeroControllerOwnerShip
=
_heroControllerFromScope
!.
_
navigator
==
this
;
final
bool
hasHeroControllerOwnerShip
=
_heroControllerFromScope
!.
navigator
==
this
;
if
(!
hasHeroControllerOwnerShip
||
previousOwner
.
_heroControllerFromScope
==
newHeroController
)
{
final
NavigatorState
otherOwner
=
hasHeroControllerOwnerShip
?
previousOwner
:
_heroControllerFromScope
!.
_
navigator
!;
:
_heroControllerFromScope
!.
navigator
!;
FlutterError
.
reportError
(
FlutterErrorDetails
(
exception:
FlutterError
(
...
...
@@ -3386,12 +3396,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
}
return
true
;
}());
newHeroController
.
_navigator
=
this
;
NavigatorObserver
.
_navigators
[
newHeroController
]
=
this
;
}
// Only unsubscribe the hero controller when it is currently subscribe to
// this navigator.
if
(
_heroControllerFromScope
?.
_
navigator
==
this
)
{
_heroControllerFromScope
?.
_navigator
=
null
;
if
(
_heroControllerFromScope
?.
navigator
==
this
)
{
NavigatorObserver
.
_navigators
[
_heroControllerFromScope
!]
=
null
;
}
_heroControllerFromScope
=
newHeroController
;
_updateEffectiveObservers
();
...
...
@@ -3440,11 +3450,11 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
}());
if
(
oldWidget
.
observers
!=
widget
.
observers
)
{
for
(
final
NavigatorObserver
observer
in
oldWidget
.
observers
)
{
observer
.
_navigator
=
null
;
NavigatorObserver
.
_navigators
[
observer
]
=
null
;
}
for
(
final
NavigatorObserver
observer
in
widget
.
observers
)
{
assert
(
observer
.
navigator
==
null
);
observer
.
_navigator
=
this
;
NavigatorObserver
.
_navigators
[
observer
]
=
this
;
}
_updateEffectiveObservers
();
}
...
...
@@ -3489,7 +3499,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
@override
void
deactivate
()
{
for
(
final
NavigatorObserver
observer
in
_effectiveObservers
)
{
observer
.
_navigator
=
null
;
NavigatorObserver
.
_navigators
[
observer
]
=
null
;
}
super
.
deactivate
();
}
...
...
@@ -3499,7 +3509,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
super
.
activate
();
for
(
final
NavigatorObserver
observer
in
_effectiveObservers
)
{
assert
(
observer
.
navigator
==
null
);
observer
.
_navigator
=
this
;
NavigatorObserver
.
_navigators
[
observer
]
=
this
;
}
}
...
...
@@ -3512,7 +3522,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
}());
assert
(()
{
for
(
final
NavigatorObserver
observer
in
_effectiveObservers
)
{
assert
(
observer
.
_
navigator
!=
this
);
assert
(
observer
.
navigator
!=
this
);
}
return
true
;
}());
...
...
packages/flutter/test/widgets/navigator_test.dart
View file @
2a6f9ad6
...
...
@@ -3865,6 +3865,39 @@ void main() {
await
tester
.
pumpAndSettle
();
expect
(
focusNode
.
hasFocus
,
true
);
});
testWidgets
(
'class implementing NavigatorObserver can be used without problems'
,
(
WidgetTester
tester
)
async
{
final
_MockNavigatorObserver
observer
=
_MockNavigatorObserver
();
Widget
build
([
Key
?
key
])
{
return
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Navigator
(
key:
key
,
observers:
<
NavigatorObserver
>[
observer
],
onGenerateRoute:
(
RouteSettings
settings
)
{
return
PageRouteBuilder
<
void
>(
settings:
settings
,
pageBuilder:
(
BuildContext
_
,
Animation
<
double
>
__
,
Animation
<
double
>
___
)
{
return
Container
();
},
);
},
),
);
}
await
tester
.
pumpWidget
(
build
());
observer
.
_checkInvocations
(<
Symbol
>[
#navigator
,
#didPush
]);
await
tester
.
pumpWidget
(
Container
(
child:
build
()));
observer
.
_checkInvocations
(<
Symbol
>[
#navigator
,
#didPush
,
#navigator
]);
await
tester
.
pumpWidget
(
Container
());
observer
.
_checkInvocations
(<
Symbol
>[
#navigator
]);
final
GlobalKey
key
=
GlobalKey
();
await
tester
.
pumpWidget
(
build
(
key
));
observer
.
_checkInvocations
(<
Symbol
>[
#navigator
,
#didPush
]);
await
tester
.
pumpWidget
(
Container
(
child:
build
(
key
)));
observer
.
_checkInvocations
(<
Symbol
>[
#navigator
,
#navigator
]);
});
}
typedef
AnnouncementCallBack
=
void
Function
(
Route
<
dynamic
>?);
...
...
@@ -4115,3 +4148,18 @@ class ZeroDurationPageRoute extends PageRoute<void> {
@override
String
?
get
barrierLabel
=>
null
;
}
class
_MockNavigatorObserver
implements
NavigatorObserver
{
final
List
<
Symbol
>
_invocations
=
<
Symbol
>[];
void
_checkInvocations
(
List
<
Symbol
>
expected
)
{
expect
(
_invocations
,
expected
);
_invocations
.
clear
();
}
@override
Object
?
noSuchMethod
(
Invocation
invocation
)
{
_invocations
.
add
(
invocation
.
memberName
);
return
null
;
}
}
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