From 2cb652600c91494564fcc07b106c0e0d3e6720fe Mon Sep 17 00:00:00 2001 From: Amit Patil <54329870+Amitpatil215@users.noreply.github.com> Date: Tue, 8 Dec 2020 22:33:03 +0530 Subject: [PATCH] showDialog assertion for non-null builder or child property (#71838) --- packages/flutter/lib/src/material/dialog.dart | 1 + .../flutter/test/material/dialog_test.dart | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 93d5cb86b7..861ee716c0 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -973,6 +973,7 @@ Future showDialog({ Widget? child, }) { assert(child == null || builder == null); + assert(child != null || builder != null); assert(barrierDismissible != null); assert(useSafeArea != null); assert(useRootNavigator != null); diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 646f3fd7bb..2e29042af5 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -217,6 +217,57 @@ void main() { expect(materialWidget.shape, customBorder); }); + testWidgets('showDialog child and builder cannot be simultaneously defined', (WidgetTester tester) async { + late BuildContext currentBuildContext; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: Builder( + builder: (BuildContext context) { + currentBuildContext = context; + return Container(); + } + ), + ), + ), + ), + ); + + expect(() async { + showDialog( + context: currentBuildContext, + child: const Text('Child'), + builder: (BuildContext context) { + return const Text('Builder'); + }, + ); + }, throwsAssertionError); + }); + + testWidgets('showDialog child or builder must be defined', (WidgetTester tester) async { + late BuildContext currentBuildContext; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: Builder( + builder: (BuildContext context) { + currentBuildContext = context; + return Container(); + } + ), + ), + ), + ), + ); + + expect( + () => showDialog(context: currentBuildContext), + throwsAssertionError, + ); + }); + testWidgets('Simple dialog control test', (WidgetTester tester) async { await tester.pumpWidget( const MaterialApp(