Search code examples
flutterdartwidget

How do I get an Image.network to show a different image if a variable is null


Code How do I show a different image if a _readProfile is null

I tried the picture above and it is erroring out


Solution

  • You need to create a method with return 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.