如何通过 Flutter 中的 url 从网络图像中获取 Uint8List?

And*_*hko 4 dart flutter

我有图像的网络 url,我需要获取 Uint8List。我怎样才能转换它?我检查类似问题的答案,但这些方法不起作用。 如何从网络图像中获取 Flutter Uint8List?

kay*_*aya 10

尝试这个:

Uint8List bytes = (await NetworkAssetBundle(Uri.parse(url)).load(url))
    .buffer
    .asUint8List();
Run Code Online (Sandbox Code Playgroud)

  • 您好,我正在尝试在块中使用您的代码,而不是使用 http get 下载图像,因为在网络上 CORS 政策不允许这样做。在网络上时,我收到“BlocError:不支持的操作:Platform._version”。知道为什么吗?非常感谢。 (3认同)

And*_*hko 5

Uint8List yourVar;
final DecoderCallback callback = (Uint8List bytes, {int cacheWidth, int cacheHeight}) {
        yourVar = bytes.buffer.asUint8List();
        return instantiateImageCodec(bytes, targetWidth: cacheWidth, targetHeight: cacheHeight);
      };
ImageProvider provider = NetworkImage(yourImageUrl);
    provider.obtainKey(createLocalImageConfiguration(context)).then((key) {
      provider.load(key, callback);
    });
Run Code Online (Sandbox Code Playgroud)