How do I show a different image if a _readProfile is null
I tried the picture above and it is erroring out
4
You need to create a method with return Widget.
Widget
And it will work as below
Widget getImage() { if (_readProfile != null) { return Image.network('YOUR LINK'); } else { return Image.asset('URL FROM ASSET FOLDER'); } }
Then you just need to call this method
Container( child: getImage(), )
It will work for you.
You can do it in one line by using ?? – if null operator
Image.network(_readProfile?.image.toString() ?? 'Your link')
add a default image in you app for exemple
const String _defaultImage = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQK_rZWRT_RXyeRBWmGHQB52XAdSfSFFbIMHyCqWZ0&s";
now you can use it in Image.network
Image.network( a ?? _defaultImage, ),
and you can add a default icon if the error will come from the server
Image.network( a ?? _defaultImage, errorBuilder: (context, error, stackTrace) => const Icon(Icons.person)), ),
You can use errorBuilder property:
Image.network( "Image URL", errorBuilder: (context, error,_){ return Image.asset("Image URL"); }, );
Click here to cancel reply.
4
Answers
You need to create a method with return
Widget
.And it will work as below
Then you just need to call this method
It will work for you.
You can do it in one line by using ?? – if null operator
add a default image in you app
for exemple
now you can use it in Image.network
and you can add a default icon if the error will come from the server
You can use errorBuilder property: