skip to Main Content

Nothing was causing any problems but I was kind of curious…

In flutter, if you write a code like this:

child: Container(
         height: 20,
         child: Container(), // could be any widget
       ),

it gives a warning of SizedBox for whitespace..

But why does this warning not show when I do this?

child: Container()

I see many codes where Container() is returned when not to show something.

2

Answers


  1. The reason why empty Container() does not raise a warning is because it is a valid widget, and no errors are raised when it is called. This is because an empty container is an empty space, and this is a valid use case in UI design. Additionally, Flutter is designed to be flexible and is designed to be able to handle all sorts of UI designs. Therefore, Flutter does not raise a warning for empty Container().

    Login or Signup to reply.
  2. Flutter’s new version suggests using SizedBox for whitespace (spaces that contain nothing or only a given width or height and nothing else).

    This way you can have a clean specific function for your widget.

    With the Container widget, you are more functionality like color, decoration etc.c

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search