Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter generate list of widget in ternary operator prompt "Expected an Identifier"

I am trying to generate a list of widget after a null check for my object, but I get an error of "Expected an Identifier". The below is my example code:

Column(
  children: [
    <condition>
    ? for (var A in <List of object>) <MyWidget>
    : const SizedBox(height: 20, width: 20)
  ],
),

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In your code, you’re using an invalid syntax. To fix this issue and achieve your goal, you can use the if statement to conditionally generate widgets within your Column. Here’s an example of how to do this:

Column(
  children: [
    if (<condition>) ... [
      for (var A in <List of object>) ... [
        MyWidget(),
      ],
    ] else ... [
      const SizedBox(height: 20, width: 20),
    ]
  ],
)

In this code:

Replace with your actual condition.

Replace with the list of objects you want to iterate over.

Replace with the widget you want to generate for each item in the list.

The if statement is used to conditionally include either the for loop or the SizedBox in the Column based on your condition. Make sure to replace placeholders with your actual code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading