0

If try to add an imagen in dart-flutter like this, the code may broke

Fo dart version Flutter 3.1.0-0.0.pre.2331
Dart Dart SDK version: 2.17.6

I solve the Fix using this:

class Movie {
      String? posterAsset;
    
      //Constructor
      Movie({
        this.posterAsset,
      });
    }
    
    child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            //imagen
            children: <Widget>[
              Image(image: AssetImage(movie.posterAsset)),
    ],
          ),
    //Inicializador
      MovieMiddle(this.movie);
    
      @override
      Widget build(BuildContext context) {
        final bordeSide = BorderSide(
          color: Color.fromARGB(20, 255, 255, 255),
          width: 1,
        );
        return Container(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            //imagen
            children: <Widget>[
              Image(image: AssetImage(movie.posterAsset!)),
            ],
          ),
        );
      }
    }



the problem is how call the image is the object is null-safety how you can see here
image: AssetImage(movie.posterAsset!),

return Container(
  child: Row(
    crossAxisAlignment: CrossAxisAlignment.start,
    //imagen
    children: <Widget>[
      Image(
        image: AssetImage(
          movie.posterAsset!,
        ),
        fit: BoxFit.cover,
      ),
    ],
  ),
);

The most import is, how manage the image:

Fo dart version Flutter 3.1.0-0.0.pre.2331
Dart Dart SDK version: 2.17.6

See the picture -> How Prepair and config the model

See the picture -> How how implement the image

CrltsMrtnz
  • 49
  • 1
  • 5
  • If you have solved your question: Could you write an answer and accept it? That way, the question is not listed as unanswered anymore – jraufeisen Aug 25 '22 at 20:49

1 Answers1

1

It is risky to use ! directly without null check, you can do

if( movie.posterAsset!=null) Image(image: AssetImage(movie.posterAsset!))

Do a null check, then use the variable like this to all place.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56