1

I want to use if statement with container widget like this code :

if (int.parse(m_id) > int.parse(cm_id) || int.parse(d_id) > 4)
    {
      Container(
         width: 0.23 * size,
         height: 0.23 * size,
         child: Image.asset('assets/images/days/day4k.png'),
     ),
}

But it gets error :

The element type 'Set<Container>' can't be assigned to the list type 'Widget'

is there a way to fix this error ?

2 Answers2

2

Braces should not be used in if the if statement is inside a flutter widget

if (int.parse(m_id) > int.parse(cm_id) || int.parse(d_id) > 4)
      Container(
         width: 0.23 * size,
         height: 0.23 * size,
         child: Image.asset('assets/images/days/day4k.png'),
   ),

The above should work just fine

If you do happen do have an if else statement, you still won't employ the use of braces, just remove the , at the end of the of the first widget

if (int.parse(m_id) > int.parse(cm_id) || int.parse(d_id) > 4)
    Container(
       width: 0.23 * size,
       height: 0.23 * size,
       child: Image.asset('assets/images/days/day4k.png'),
    ) //no comma here
else
    Container(), //comma here

Also, If you have an else if statement, you would also approach it in the same way

if (int.parse(m_id) > int.parse(cm_id) || int.parse(d_id) > 4)
    Container(
       width: 0.23 * size,
       height: 0.23 * size,
       child: Image.asset('assets/images/days/day4k.png'),
    ) //no comma here
else if (condition here)
    Container() //no comma here
else
    Container(), //comma here
ByteMe
  • 1,575
  • 1
  • 12
  • 23
0

It's must works:

if (int.parse(m_id) > int.parse(cm_id) || int.parse(d_id) > 4)...[
    Container(
         width: 0.23 * size,
         height: 0.23 * size,
         child: Image.asset('assets/images/days/day4k.png'),
     ),
]
Captivity
  • 279
  • 5
  • 21