Reputation: 164
I am trying to make a self within my children, but this is giving me an error if I make mistakes within agitated children. What am I doing wrong?
What I'm wanting to do is:
if(snapshot.data.Facebook != null){
// display the image if different from null
Image.asset("assets/images/facebook.png",height: 40,), SizedBox(width: 10),
}
My code: ----------------------------------------------------------
Padding(
padding: EdgeInsets.only(top: 50),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if(snapshot.data.Facebook != null){
};
Image.asset("assets/images/facebook.png",height: 40,), SizedBox(width: 10),
Image.asset("assets/images/instagram.png", height: 40,), SizedBox(width: 10),
Image.asset("assets/images/linkdin.png", height: 40,), SizedBox(width: 10),
Image.asset("assets/images/pinterest.png", height: 40,), SizedBox(width: 10),
Image.asset("assets/images/skype.png", height: 40,), SizedBox(width: 10),
Image.asset("assets/images/twitter.png", height: 40,), SizedBox(width: 10),
Image.asset("assets/images/whatsapp.png", height: 40,), SizedBox(width: 10),
],
),
)
Upvotes: 0
Views: 75
Reputation: 4392
You have to create a function which returns list of Widgets and add that function into your rows. Something like
UPDATE:
List<Widget> populateRow() {
if(snapshot.data.Facebook != null){
return [Image.asset("assets/images/facebook.png",height: 40,)],
} else if (snapshot.data.Instagram != null){
return [Image.asset("assets/images/instagram.png",height: 40,)],
} ...
return [];
}
and in your row add the function like this
children: populateRow()
Please check if it works. I haven't test it. And obviously you can modify the function to display whatever images you want
Upvotes: 1
Reputation: 162
Here is a version of if/else inside widget. If the condition is true, widget in front of ?
will be showed and :
for false condition.
Padding(
padding: EdgeInsets.only(top: 50),
child: snapshot.data.Facebook != null //check the condition
//return Row with images in it if snapshot.data.Facebook != null is true
? Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.asset(
"assets/images/facebook.png",
height: 40,
),
SizedBox(width: 10),
Image.asset(
"assets/images/instagram.png",
height: 40,
),
SizedBox(width: 10),
Image.asset(
"assets/images/linkdin.png",
height: 40,
),
SizedBox(width: 10),
Image.asset(
"assets/images/pinterest.png",
height: 40,
),
SizedBox(width: 10),
Image.asset(
"assets/images/skype.png",
height: 40,
),
SizedBox(width: 10),
Image.asset(
"assets/images/twitter.png",
height: 40,),
SizedBox(width: 10),
Image.asset(
"assets/images/whatsapp.png",
height: 40,
),
SizedBox(width: 10),
],
)
//return empty Row if snapshot.data.Facebook != null is false
: Row(
children: <Widget>[
//you can add more widget in here
],
)
)
Upvotes: 0