0

我需要在我的 Flutter 应用程序中显示 gif。从后端,我从响应中获取 gif 作为 Uint8List 列表。你能帮我吗?我怎样才能在屏幕上显示这个?

我的代码在这里:

widget.session
        .get('/api/caff/getCaff/' + widget.gifId.toString())
        .then((response) async {
      if (response.statusCode == 200) {
        Uint8List bytes = response.bodyBytes;
        _gifFile = File.fromRawPath(bytes); // tried this but didn't work
      } else {
        CaffToast.showError(
            'Something went wrong! Please check your network connection!');
      }
    });

我试图将其显示为文件图像,但它没有工作:

@override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          _gifFile == null ? Container() : Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: FileImage(_gifFile!))),
          ),
        ],
      ),
    );
  }

你有什么建议我该如何解决这个问题?

4

1 回答 1

0

无需将数据写入文件。图像数据已经在内存中,所以只需显示它。只需使用MemoryImage图像提供程序

我不确定你是如何从网络调用中获取数据到你的build方法的,所以我使用占位符,但就像你在使用文件时所做的那样。

_bytes = response.bodyBytes;
@override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          _gifFile == null ? Container() : Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: MemoryImage(_bytes!))),
          ),
        ],
      ),
    );
  }
于 2021-12-05T20:38:23.717 回答