From 2665a72559501be9f5558b85675d66506ce729d3 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 21 Oct 2024 20:26:09 +0300 Subject: [PATCH] Fix unattached `MenuController` throws assertion error when calling `MenuController.close()` (#156588) Fixes [Closing a menu controller that was never attached throws an assertion error](https://github.com/flutter/flutter/issues/156572) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: ElevatedButton( onPressed: () => Navigator.of(context).push( MaterialPageRoute( builder: (context) => const MenuControllerPage(), ), ), child: const Text('Press Me'), ), ), ); } } class MenuControllerPage extends StatefulWidget { const MenuControllerPage({super.key}); @override State createState() => _MenuControllerPageState(); } class _MenuControllerPageState extends State { // This menu controller is intentionally left as not attached to a MenuAnchor. final MenuController _menuController = MenuController(); @override void dispose() { // Close the menu when the page is popped. _menuController.close(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: ElevatedButton( child: const Text('Go back'), onPressed: () => Navigator.of(context).pop(), ), ), ); } } ```
--- packages/flutter/lib/src/material/menu_anchor.dart | 3 +-- packages/flutter/test/material/menu_anchor_test.dart | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index b29fb447ce..3ce62dbe28 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -664,8 +664,7 @@ class MenuController { /// scrolled by an ancestor, or the view changes size, then any open menu will /// automatically close. void close() { - assert(_anchor != null); - _anchor!._close(); + _anchor?._close(); } /// Opens the menu that this menu controller is associated with. diff --git a/packages/flutter/test/material/menu_anchor_test.dart b/packages/flutter/test/material/menu_anchor_test.dart index 4e5e204141..659ade774c 100644 --- a/packages/flutter/test/material/menu_anchor_test.dart +++ b/packages/flutter/test/material/menu_anchor_test.dart @@ -4718,6 +4718,14 @@ void main() { tester.renderObject(find.byWidget(rootOverlay)), ); }); + + // Regression test for https://github.com/flutter/flutter/issues/156572. + testWidgets('Unattached MenuController does not throw when calling close', (WidgetTester tester) async { + final MenuController controller = MenuController(); + controller.close(); + await tester.pump(); + expect(tester.takeException(), isNull); + }); } List createTestMenus({