'Multiple if statements in a flutter image widget

enter image description here

this is my code in flutter, I'm trying to display an image using 2 if conditions based on the chosen currency, I'm not getting the correct output. i know something is wrong in the code.



Solution 1:[1]

you are using the tenary expression too complicated, i would suggest you to use function instead:

String _cryptoImage(String cryptoName){
  String imageUrl = coinList[3].imageUrl;

  if(cryptoName == 'btc') imageUrl = coinList[0];
  else if(cryptoName == 'eth') imageUrl = coinList[1];

  return imageUrl;
}

CachedNetworkImage(
  imageUrl: _cryptoImage(ownedCryptoDB[index])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jim