Home > Back-end >  Dart: The argument type 'Image?' can't be assigned to the parameter type 'Image&
Dart: The argument type 'Image?' can't be assigned to the parameter type 'Image&

Time:02-25

What is the meaning of this error and how to solve it?

Error:

Animation? decodeImage
The argument type 'Image?' can't be assigned to the parameter type 'Image'

Dart Code:

import 'dart:io';
import 'package:image/image.dart';

void main(){

  final image = 'asset/test.webp';

  final bytes = File(image).readAsBytesSync(); // type List<int>
  final decodeImage = decodeWebPAnimation(bytes); // type Animation

  var obj = WebPEncoder(); 

  obj.addFrame(decodeImage?[0]); // gives error

}

What are the methods to fix it?

CodePudding user response:

Try making it non-null bye adding the bang (!) like so:

  obj.addFrame(decodeImage?[0]!);

CodePudding user response:

I fixed it by using as [type]

obj.addFrame(decodeImage?[0] as Image);

Ref: https://stackoverflow.com/a/68969699/9308731

  • Related