Repository navigation
MenuItemButton may throw when updated with a null FocusNode #142095
Copy link
Copy link
Closed
Labels
a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworkfound in release: 3.16Found to occur in 3.16Found to occur in 3.16found in release: 3.19Found to occur in 3.19Found to occur in 3.19frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamOwned by Design Languages team
Description
On the MenuItemButton widget, if widget.focusNode is updated to null, didUpdateWidget would throw.
Steps to reproduce
Change menu item focus node to null
Expected outcome
Doesn't throw
Actual outcome
Throws
Code sample
import 'package:flutter/material.dart';
/// Flutter code sample for [MenuAnchor].
void main() => runApp(const MenuApp());
class MenuApp extends StatelessWidget {
const MenuApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: const Scaffold(body: SafeArea(child: MyCascadingMenu())),
);
}
}
class MyCascadingMenu extends StatefulWidget {
const MyCascadingMenu({super.key});
@override
State<MyCascadingMenu> createState() => _MyCascadingMenuState();
}
class _MyCascadingMenuState extends State<MyCascadingMenu> {
late FocusNode? _buttonFocusNode = FocusNode();
@override
Widget build(BuildContext context) {
return MenuAnchor(
menuChildren: <Widget>[
MenuItemButton(
focusNode: _buttonFocusNode,
closeOnActivate: false,
child: Text("Set focus to null"),
onPressed: () => setState(() {
_buttonFocusNode = null;
})),
],
builder:
(BuildContext context, MenuController controller, Widget? child) {
return TextButton(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('OPEN MENU'),
);
},
);
}
}Here is a gist demonstrating the bug in action on the Master branch: https://dartpad.dev/?id=a7da3f81557b11f7080b1f5a8afa0779&channel=master
Here is the code in the master material library: https://github.com/flutter/flutter/blob/b5262f0d80eb6da1b1cd6ceff0943367fbc29331/packages/flutter/lib/src/material/menu_anchor.dart#L1078C8-L1078C53
The code throws on both master and stable branches.
Relevate framework code
class _MenuItemButtonState extends State<MenuItemButton> {
FocusNode? _internalFocusNode;
FocusNode get _focusNode => widget.focusNode ?? _internalFocusNode!;
// ...
@override
void didUpdateWidget(MenuItemButton oldWidget) {
if (widget.focusNode != oldWidget.focusNode) {
// **BUG?** If widget.focusNode changes to null, this appears to throw
_focusNode.removeListener(_handleFocusChange);
if (widget.focusNode != null) {
_internalFocusNode?.dispose();
_internalFocusNode = null;
}
_createInternalFocusNodeIfNeeded();
_focusNode.addListener(_handleFocusChange);
}
super.didUpdateWidget(oldWidget);
}
// ...
void _createInternalFocusNodeIfNeeded() {
if (widget.focusNode == null) {
_internalFocusNode = FocusNode();
assert(() {
if (_internalFocusNode != null) {
_internalFocusNode!.debugLabel = '$MenuItemButton(${widget.child})';
}
return true;
}());
}
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworkfound in release: 3.16Found to occur in 3.16Found to occur in 3.16found in release: 3.19Found to occur in 3.19Found to occur in 3.19frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamOwned by Design Languages team
Activity
huycozy commentedon Jan 24, 2024
Hi @davidhicks980
The goal of this Github repo is not to give assistance on personal code, but to improve flutter reporting bugs or feature requests.
If you encounter behavior that you believe could be a Flutter framework bug, please try to provide all information indicated in the template so that we can address this issue properly (See bug issue template here).
Instead, if you think that the issue might depend on your implementation, please see flutter's community for resources and ask questions like this. You may also get some help if you post it on Stack Overflow and flutterhelp.
davidhicks980 commentedon Jan 24, 2024
@huycozy
My bad, I came across it while working on a different piece of code and wanted to make sure to flag it, but I should have filed a full report.
Steps to reproduce
Change menu item focus node to null
Expected outcome
Doesn't throw
Actual outcome
Throws
Code sample
Here is a gist demonstrating the bug in action on the Master branch: https://dartpad.dev/?id=a7da3f81557b11f7080b1f5a8afa0779&channel=master
Here is the code in the master material library: https://github.com/flutter/flutter/blob/b5262f0d80eb6da1b1cd6ceff0943367fbc29331/packages/flutter/lib/src/material/menu_anchor.dart#L1078C8-L1078C53
The code throws on both master and stable branches.
I updated the original question with this text as well.
huycozy commentedon Jan 25, 2024
Thanks for the update. Also, I notice this issue only occurs if setting
closeOnActivatetofalse.Output log
flutter doctor -v (stable and master)
[✓] Flutter (Channel stable, 3.16.8, on macOS 14.1 23B74 darwin-x64, locale en-VN) • Flutter version 3.16.8 on channel stable at /Users/huynq/Documents/GitHub/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 67457e669f (27 hours ago), 2024-01-16 16:22:29 -0800 • Engine revision 6e2ea58a5c • Dart version 3.2.5 • DevTools version 2.28.5 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15A507 • CocoaPods version 1.14.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.1) • Android Studio at /Applications/Android Studio Hedgehog.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) [✓] Android Studio (version 2022.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = /Applications/Android Studio.app/ • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) [✓] VS Code (version 1.85.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.80.0 [✓] Connected device (3 available) • iPhone (mobile) • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios • iOS 15.8 19H370 • macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.234 [✓] Network resources • All expected network resources are available. • No issues found!davidhicks980 commentedon Jan 25, 2024
I think that's because the MenuItem is unmounted before a new FocusNode is set, but I'll defer to you all on that.
Fix null operator error when tapping on 'MenuItemButton' (#142230)
github-actions commentedon Feb 15, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.