首页 > 解决方案 > _Callback<>、_toByteData() 和 _futurize() 未定义

问题描述

我在 Flutter 中遇到了这个问题:

Error: The method '_futurize' isn't defined for the class '_MainPage'.

Error: The method '_toByteData' isn't defined for the class '_MainPage'

Error: '_Callback' isn't a type.

这在https://api.flutter.dev/flutter/dart-ui/Image/toByteData.html中提到的函数中,用于获取图像的二进制值。

实际代码是:

Future<ByteData?> toByteData({ImageByteFormat format = ImageByteFormat.rawRgba}) {
  return _futurize((_Callback<ByteData> callback) {
    return _toByteData(format.index, (Uint8List? encoded) {
      callback(encoded!.buffer.asByteData());
    });
  });
}

标签: flutterdart

解决方案


您引用的代码代表代码的实现方式。

出于这个原因,_futurizeand_toByteData方法是private类的Image,不能从外部访问,因此会出现错误。

如果您想使用该toByteData()功能,您可以简单地使用如下代码:

ByteData byteData = await image.toByteData();

推荐阅读