GAGAN SINGH
GAGAN SINGH

Reputation: 281

how to hide this array sign ' [] ' when it is null?

I am calling a list of array from firestore and wants to hide this '[]' when it is null or I want to display some other text if it is null but it gets this array sign when there is no data. Thanks. Pic attached:-enter image description here

The code:-

    child:Align(
                        alignment: Alignment.centerLeft,
                        child: Text(hobbies != null?
                        "$hobbies":'Add what you love to do.....',
                          style: TextStyle(fontSize: 16,),
                          textAlign: TextAlign.start,)
                    )

Upvotes: 0

Views: 59

Answers (2)

Maxouille
Maxouille

Reputation: 2911

Your array isn't null, you should check if it's empty. That's why you have an empty array symbol and not an error.

I'm not sure if it's mandatory to check if hobbies is null. Indeed, I think Firebase return an empty array is there is a key but no value in the JSON. To be sure, you can add the null check.

child:Align(
    alignment: Alignment.centerLeft,
    child: Text(hobbies != null && hobbies.isEmpty ? 'Add what you love to do.....' 
    : "$hobbies",
    style: TextStyle(fontSize: 16,),
    textAlign: TextAlign.start,)
)

Best

Upvotes: 0

KuKu
KuKu

Reputation: 7512

As Maxouille said,
you would better check null and empty.

child:Align(
                        alignment: Alignment.centerLeft,
                        child: Text(hobbies != null && hobbies.isNotEmpty?
                        "$hobbies":'Add what you love to do.....' : '',
                          style: TextStyle(fontSize: 16,),
                          textAlign: TextAlign.start,)
                    )

Upvotes: 1

Related Questions