Make CupertinoNavigationBarBackButton correctly return an assert error (#30815)

This commit is contained in:
xster 2019-04-15 15:33:33 -07:00 committed by GitHub
parent d628537172
commit 364d73c7d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -1231,7 +1231,7 @@ class CupertinoNavigationBarBackButton extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final ModalRoute<dynamic> currentRoute = ModalRoute.of(context); final ModalRoute<dynamic> currentRoute = ModalRoute.of(context);
assert( assert(
currentRoute.canPop, currentRoute?.canPop == true,
'CupertinoNavigationBarBackButton should only be used in routes that can be popped', 'CupertinoNavigationBarBackButton should only be used in routes that can be popped',
); );

View File

@ -883,6 +883,28 @@ void main() {
); );
expect(SystemChrome.latestStyle, SystemUiOverlayStyle.dark); expect(SystemChrome.latestStyle, SystemUiOverlayStyle.dark);
}); });
testWidgets('CupertinoNavigationBarBackButton shows an error when manually added outside a route', (WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoNavigationBarBackButton()
);
final dynamic exception = tester.takeException();
expect(exception, isAssertionError);
expect(exception.toString(), contains('CupertinoNavigationBarBackButton should only be used in routes that can be popped'));
});
testWidgets('CupertinoNavigationBarBackButton shows an error when placed in a route that cannot be popped', (WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoApp(
home: CupertinoNavigationBarBackButton(),
),
);
final dynamic exception = tester.takeException();
expect(exception, isAssertionError);
expect(exception.toString(), contains('CupertinoNavigationBarBackButton should only be used in routes that can be popped'));
});
} }
class _ExpectStyles extends StatelessWidget { class _ExpectStyles extends StatelessWidget {