Home > Mobile >  Should I use null or an empty Container when I don't want to render a widget according to the a
Should I use null or an empty Container when I don't want to render a widget according to the a

Time:07-08

I want to use a FloatingActionButton only if the platform is Android:

floatingActionButton: Platform.isIOS
  ? null
  : FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: ...
    ),

Should I render an empty container instead? How should I deal with widgets that I don't want under specific conditions overall? Is it bad convention to use null widgets?

CodePudding user response:

Not bad convention to use null widgets.

Check from Flutter's source code, you could see the following:


class Scaffold extends StatefulWidget {
  /// Creates a visual scaffold for material design widgets.
  const Scaffold({
    Key? key,
    this.appBar,
    this.body,
    this.floatingActionButton,
    ...
   }

    final Widget? floatingActionButton;

}

floatingActionButton is optional as designed.

If you not use the property floatingActionButton , floatingActionButton is null too.


So not need render an empty container instead.To deal with widgets that I don't want under specific conditions overall, it is ok to use null widgets

  • Related