【问题标题】:Dart: The argument type 'Image?' can't be assigned to the parameter type 'Image' [duplicate]Dart:参数类型“图像?”不能分配给参数类型“图像”[重复]
【发布时间】:2022-02-25 02:01:15
【问题描述】:

这个错误是什么意思,如何解决?

错误:

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

飞镖代码:

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

}

修复方法有哪些?

【问题讨论】:

    标签: android dart webp animated-webp


    【解决方案1】:

    试着让它非空再见,像这样添加bang (!):

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

    【讨论】:

    • 感谢您的回复。试过(!)。但它说它不会有效果。
    • 好的解决了。我使用“作为图像”
    • 改用decodedImage![0]。 (然后担心你是否真的可以依赖decodedImage 而不是null)。
    【解决方案2】:

    我使用as [type]修复了它

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

    参考:https://stackoverflow.com/a/68969699/9308731

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2020-04-15
      • 2021-09-15
      • 2019-12-06
      • 1970-01-01
      • 2021-10-04
      • 2021-08-06
      • 2021-09-16
      • 1970-01-01
      相关资源
      最近更新 更多