Home > Software design >  How add null widget in column in flutter?
How add null widget in column in flutter?

Time:09-14

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 ?

CodePudding user response:

Try

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

CodePudding user response:

As per your code try to declare the values of String and double variables like below or main thing null is not type of Widget so declare return type is Widget only

your function:

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

Your Widget:

Column(
  children:[
   myWidget('M', 12.0),
  ]
) 

Refer my same answer image

  • Related