Reputation: 281
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:-
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
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
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