flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.18k stars 26.64k forks source link

MenuItemButton error if child is null #147479

Closed javaone199 closed 2 weeks ago

javaone199 commented 2 weeks ago

Steps to reproduce

MenuItemButton( onPressed: () {}, leadingIcon: Icon(Icons.any), child: null, );

Null check operator used on a null value

Expected results

The child is nullable. If it is null, the MenuItemButton should be the same as IconButton.

Actual results

Null check operator used on a null value

Code sample

Code sample ```dart MenuItemButton( onPressed: () {}, leadingIcon: Icon(Icons.any), child: null, ); ```

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor output ```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.19.5, on Microsoft Windows [Version 10.0.22631.3447], locale en-US) [✓] Windows Version (Installed version of Windows is version 10 or higher) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2) [✓] Chrome - develop for the web [✓] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.4.4) [✓] Android Studio (version 2022.1) [✓] Connected device (3 available) [✓] Network resources • No issues found! ```
wuweijian1997 commented 2 weeks ago

but it's required

  const MenuItemButton({
    super.key,
    this.onPressed,
    this.onHover,
    this.requestFocusOnHover = true,
    this.onFocusChange,
    this.focusNode,
    this.shortcut,
    this.style,
    this.statesController,
    this.clipBehavior = Clip.none,
    this.leadingIcon,
    this.trailingIcon,
    this.closeOnActivate = true,
    required this.child,
  });
wuweijian1997 commented 2 weeks ago

child cannot be null.

class MenuItemButton extends StatefulWidget {
  @override
  State<MenuItemButton> createState() => _MenuItemButtonState();
}

class _MenuItemButtonState extends State<MenuItemButton> {
  @override
  Widget build(BuildContext context) {
     Widget child = TextButton(
      onPressed: widget.enabled ? _handleSelect : null,
      onHover: widget.enabled ? _handleHover : null,
      onFocusChange: widget.enabled ? widget.onFocusChange : null,
      focusNode: _focusNode,
      style: mergedStyle,
      statesController: widget.statesController,
      clipBehavior: widget.clipBehavior,
      isSemanticButton: null,
      child: _MenuItemLabel(
        leadingIcon: widget.leadingIcon,
        shortcut: widget.shortcut,
        trailingIcon: widget.trailingIcon,
        hasSubmenu: false,
        child: widget.child!,  <<<<<<<<<<< look here. can't null
      ),
    );
  }
}
huycozy commented 2 weeks ago

Hi @javaone199 I'm quite inclined to mark this as a proposal instead since the child property is required by design. I wonder what the use case is although you can pass an empty widget (SizedBox.shrink() or Container()) to child, right?

javaone199 commented 2 weeks ago

No compilation error if passing null to child. MenuItemButton = IconButton + TextButton.

I am using different types of buttons by checking if icon or text is null.

javaone199 commented 2 weeks ago

This issue is minor and can be closed.

javaone199 commented 2 weeks ago

From VSC, the child parameter is defined as { required Widget? child}. Should it be {required Widget child}? If this is the case, this issue should be re-opened. :)