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

How to make a Flutter container conditional?

I have String variable ‘sdesc1’. Now this variable could either have a value or it could be empty. I am using a container to display the above variable.

....
,
Container(
  width: double.infinity,
  color: Colors.transparent,
  child: Padding(
    padding: const EdgeInsets.all(8.0),
    child: Text(sdesc1,
        style:
            TextStyle(fontSize: 10, fontStyle: FontStyle.italic)),
  ),
),
....

In the app screen, the above code generates a blank row occupying some height when the variable ‘sdesc1’ is empty which is not desirable.

I tried to move the piece of code in a widget method and return the container, but it showed me an error saying that return type could be null some thing like that.

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

How can I make a container conditional when the variables are empty. Please help with some codes. Thanks in advance.

>Solution :

This condition in the build method can prevents from creating a container (I guessed sdesc1 was a string):

if(sdesc1.isNotEmpty)
    Container(
      width: double.infinity,
      color: Colors.transparent,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(sdesc1,
            style:
                TextStyle(fontSize: 10, fontStyle: FontStyle.italic)),
      ),
    ),
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