Search code examples
flutterdartdart-null-safety

The operand can't be null, so the condition is always true. Remove the condition


I keep getting this warning and I am unsure how to resolve it. Help if u can please Below is the excerpt of the code

String imageUrl = '';

CircleAvatar(
     backgroundColor: Colors.white70,
     backgroundImage: imageUrl != null
    ? Image.network(imageUrl).image
      : AssetImage(ProjectImages.placeholder),
        minRadius: 50.0,
     ),

Solution

  • You are getting this warning because you are declaring your variable "imageUrl" with and empty String.

    Note: Empty string and null both are not same they are different. So, to get rid of this warning you have to make your variable nullable like this

    String? imageUrl;
    
    CircleAvatar(
         backgroundColor: Colors.white70,
         backgroundImage: imageUrl != null
        ? Image.network(imageUrl).image
          : AssetImage(ProjectImages.placeholder),
            minRadius: 50.0,
         ),
    

    Your warning will be gone :)