Unverified Commit 431dfc83 authored by fzyzcjy's avatar fzyzcjy Committed by GitHub

Tiny cleanup for Navigator code (without introducing dependency) (#125628)

Close https://github.com/flutter/flutter/issues/125724

> The `navigator.dart` has ~10 repeats of things like:
> 
> ```dart
>     final _RouteEntry? currentRouteEntry = _navigator!._history.cast<_RouteEntry?>().lastWhere(
>       (_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
>       orElse: () => null,
>     );
> ```
> 
> while it can be greatly simplified as:
> 
> ```dart
>     final _RouteEntry? currentRouteEntry = _navigator!._history.lastWhereOrNull(_RouteEntry.isPresentPredicate);
> ```
> 
> Thus, it seems that we can beautify the code a little bit.

Same as https://github.com/flutter/flutter/pull/125099, but without external dependency

For more detailed explanation why it does not introduce external dependency: https://github.com/flutter/flutter/pull/125099#discussion_r1174230502
parent e11a2213
...@@ -464,10 +464,7 @@ abstract class Route<T> { ...@@ -464,10 +464,7 @@ abstract class Route<T> {
if (_navigator == null) { if (_navigator == null) {
return false; return false;
} }
final _RouteEntry? currentRouteEntry = _navigator!._history.cast<_RouteEntry?>().lastWhere( final _RouteEntry? currentRouteEntry = _navigator!._lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
);
if (currentRouteEntry == null) { if (currentRouteEntry == null) {
return false; return false;
} }
...@@ -482,10 +479,7 @@ abstract class Route<T> { ...@@ -482,10 +479,7 @@ abstract class Route<T> {
if (_navigator == null) { if (_navigator == null) {
return false; return false;
} }
final _RouteEntry? currentRouteEntry = _navigator!._history.cast<_RouteEntry?>().firstWhere( final _RouteEntry? currentRouteEntry = _navigator!._firstRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
);
if (currentRouteEntry == null) { if (currentRouteEntry == null) {
return false; return false;
} }
...@@ -522,10 +516,7 @@ abstract class Route<T> { ...@@ -522,10 +516,7 @@ abstract class Route<T> {
if (_navigator == null) { if (_navigator == null) {
return false; return false;
} }
return _navigator!._history.cast<_RouteEntry?>().firstWhere( return _navigator!._firstRouteEntryWhereOrNull(_RouteEntry.isRoutePredicate(this))?.isPresent ?? false;
(_RouteEntry? e) => e != null && _RouteEntry.isRoutePredicate(this)(e),
orElse: () => null,
)?.isPresent ?? false;
} }
} }
...@@ -4049,9 +4040,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4049,9 +4040,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
// Announce route name changes. // Announce route name changes.
if (widget.reportsRouteUpdateToEngine) { if (widget.reportsRouteUpdateToEngine) {
final _RouteEntry? lastEntry = _history.cast<_RouteEntry?>().lastWhere( final _RouteEntry? lastEntry = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e), orElse: () => null,
);
final String? routeName = lastEntry?.route.settings.name; final String? routeName = lastEntry?.route.settings.name;
if (routeName != null && routeName != _lastAnnouncedRouteName) { if (routeName != null && routeName != _lastAnnouncedRouteName) {
SystemNavigator.routeInformationUpdated(uri: Uri.parse(routeName)); SystemNavigator.routeInformationUpdated(uri: Uri.parse(routeName));
...@@ -4929,10 +4918,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4929,10 +4918,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
/// to define the route's `willPop` method. /// to define the route's `willPop` method.
@optionalTypeArgs @optionalTypeArgs
Future<bool> maybePop<T extends Object?>([ T? result ]) async { Future<bool> maybePop<T extends Object?>([ T? result ]) async {
final _RouteEntry? lastEntry = _history.cast<_RouteEntry?>().lastWhere( final _RouteEntry? lastEntry = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
);
if (lastEntry == null) { if (lastEntry == null) {
return false; return false;
} }
...@@ -4942,10 +4928,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4942,10 +4928,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
// Forget about this pop, we were disposed in the meantime. // Forget about this pop, we were disposed in the meantime.
return true; return true;
} }
final _RouteEntry? newLastEntry = _history.cast<_RouteEntry?>().lastWhere( final _RouteEntry? newLastEntry = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
);
if (lastEntry != newLastEntry) { if (lastEntry != newLastEntry) {
// Forget about this pop, something happened to our history in the meantime. // Forget about this pop, something happened to our history in the meantime.
return true; return true;
...@@ -5029,19 +5012,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -5029,19 +5012,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
void popUntil(RoutePredicate predicate) { void popUntil(RoutePredicate predicate) {
_RouteEntry? candidate = _history.cast<_RouteEntry?>().lastWhere( _RouteEntry? candidate = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
);
while(candidate != null) { while(candidate != null) {
if (predicate(candidate.route)) { if (predicate(candidate.route)) {
return; return;
} }
pop(); pop();
candidate = _history.cast<_RouteEntry?>().lastWhere( candidate = _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate);
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
);
} }
} }
...@@ -5065,10 +5042,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -5065,10 +5042,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
}()); }());
if (wasCurrent) { if (wasCurrent) {
_afterNavigation( _afterNavigation(
_history.cast<_RouteEntry?>().lastWhere( _lastRouteEntryWhereOrNull(_RouteEntry.isPresentPredicate)?.route,
(_RouteEntry? e) => e != null && _RouteEntry.isPresentPredicate(e),
orElse: () => null,
)?.route,
); );
} }
} }
...@@ -5142,10 +5116,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -5142,10 +5116,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
@optionalTypeArgs @optionalTypeArgs
Route<T>? _getRouteById<T>(String id) { Route<T>? _getRouteById<T>(String id) {
return _history.cast<_RouteEntry?>().firstWhere( return _firstRouteEntryWhereOrNull((_RouteEntry entry) => entry.restorationId == id)?.route as Route<T>?;
(_RouteEntry? entry) => entry!.restorationId == id,
orElse: () => null,
)?.route as Route<T>?;
} }
int get _userGesturesInProgress => _userGesturesInProgressCount; int get _userGesturesInProgress => _userGesturesInProgressCount;
...@@ -5233,6 +5204,27 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -5233,6 +5204,27 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
_activePointers.toList().forEach(WidgetsBinding.instance.cancelPointer); _activePointers.toList().forEach(WidgetsBinding.instance.cancelPointer);
} }
/// Gets first route entry satisfying the predicate, or null if not found.
_RouteEntry? _firstRouteEntryWhereOrNull<T>(_RouteEntryPredicate test) {
for (final _RouteEntry element in _history) {
if (test(element)) {
return element;
}
}
return null;
}
/// Gets last route entry satisfying the predicate, or null if not found.
_RouteEntry? _lastRouteEntryWhereOrNull<T>(_RouteEntryPredicate test) {
_RouteEntry? result;
for (final _RouteEntry element in _history) {
if (test(element)) {
result = element;
}
}
return result;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
assert(!_debugLocked); assert(!_debugLocked);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment