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 ?

8
  • 1
    remove the braces Commented Aug 4, 2020 at 12:53
  • @Yann39 then if i have else statement after this? Commented Aug 4, 2020 at 12:56
  • Unfortunately you cannot have an else statement, use another if or move the code into a function returning a widget. Commented Aug 4, 2020 at 13:00
  • Thanks alot i used if statement twice and it is worked Commented Aug 4, 2020 at 13:04
  • @Yann39 you can have an else statement, check out my answer Commented Aug 4, 2020 at 13:05

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

so if i have else statement after if, how can i put else?
I just added how to do it in an if else situation
You're welcome. I've also updated my answer to show how to do in an else if statement in case you need that in the future
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'),
     ),
]

2 Comments

so if i have else statement after if, how can i put else?
else not, but you can write code like this: if (!(int.parse(m_id) > int.parse(cm_id) || int.parse(d_id) > 4))...[ this is like else ]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.