Improve error message when using popuntil with bad predicate (#57247)

This commit is contained in:
chunhtai 2020-05-27 10:02:05 -07:00 committed by GitHub
parent af9b6a6efa
commit 68037a23af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -3642,8 +3642,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
/// ```
/// {@end-tool}
void popUntil(RoutePredicate predicate) {
while (!predicate(_history.lastWhere(_RouteEntry.isPresentPredicate).route)) {
_RouteEntry candidate = _history.lastWhere(_RouteEntry.isPresentPredicate, orElse: () => null);
while(candidate != null) {
if (predicate(candidate.route))
return;
pop();
candidate = _history.lastWhere(_RouteEntry.isPresentPredicate, orElse: () => null);
}
}

View File

@ -696,6 +696,33 @@ void main() {
expect(find.text('C'), isOnstage);
});
testWidgets('Able to pop all routes', (WidgetTester tester) async {
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/' : (BuildContext context) => const OnTapPage(
id: '/',
),
'/A': (BuildContext context) => const OnTapPage(
id: 'A',
),
'/A/B': (BuildContext context) => OnTapPage(
id: 'B',
onTap: (){
// Pops all routes with bad predicate.
Navigator.of(context).popUntil((Route<dynamic> route) => false);
},
),
};
await tester.pumpWidget(
MaterialApp(
routes: routes,
initialRoute: '/A/B',
)
);
await tester.tap(find.text('B'));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
});
testWidgets('pushAndRemoveUntil triggers secondaryAnimation', (WidgetTester tester) async {
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/' : (BuildContext context) => OnTapPage(