Search code examples
flutterflutter-layout

How add null widget in column in flutter?


I have a specific widget that I want to add in column in specific condition.

If condition will grant than widget will add in column.

else I don't want to add any widget

Column(
  children:<>[
   MyWidget(str,db)
  ]
)



MyWidget(String s, double d) {
    print(s);
    switch(s){
      case 'M':
        return Container(
          color: Colors.red,
          height: d,
        );
      case 'T':
        return null;
    }
  }

Here I got error

type 'Null' is not a subtype of type 'Widget'

I understand this problem ,However I don't want to add any kind of widget to the column.

Please don't suggest

SizeBox.shrink
empty widget
Container()

Once again I don't want to add any kind of widget to the column. if condition will not satisfied , how to resolve this ?


Solution

  • Try

    Column(
      children:<>[
      if(str=='M') 
          Container(
             color: Colors.red,
             height: db,
          ),
      ]
    ),