繁体   English   中英

颤振:输入“未来”<dynamic> &#39; 不是类型 &#39;Uint8List&#39; 的子类型

[英]Flutter: type 'Future<dynamic>' is not a subtype of type 'Uint8List'

我需要在 ListTile 中显示预览图像,我使用 Uint8List。 这是我在 ListTile 中获取 Uint8List 数据的代码:

                  ConstrainedBox(
                    constraints: BoxConstraints(
                      minWidth: 44,
                      minHeight: 44,
                      maxWidth: 64,
                      maxHeight: 64,
                    ),
                    child: Image.memory(loadAsset(paths[position].fileName)),
                  ) 

获取 Uint8List 的代码片段:

  loadAsset(name) async {
    Uint8List data = (await clientNextCloud.preview.getPreview(name, 10, 10))
        .buffer
        .asUint8List();
    setState(() {});
    return data;
  }

await clientNextCloud.preview.getPreview(name, 10, 10) - 这一块,从包 nextcloud dart 中获取 Uint8List。

我收到以下错误 - type 'Future<dynamic>' is not a subtype of type 'Uint8List'有人能告诉我问题是什么吗? 谢谢!

问题在于您的loadAsset是一个异步方法,这意味着它将返回一个Future<T> 您无法更改它,因为 getPreview 也是异步的。

要解决此问题,您需要调整 UI 以在异步方法未完成时显示某些内容。 您可以使用FutureBuilder来实现这一点。

尝试使用以下方法 -

loadAsset(paths[position].fileName).then((value) => {
               Image.memory(value),               
});

loadAsset 方法返回一个 Future 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM