【问题标题】:flutter : Exception has occurred. _TypeError (type 'Future<dynamic>' is not a subtype of type 'Uint8List')颤振:发生异常。 _TypeError(类型 'Future<dynamic>' 不是类型 'Uint8List' 的子类型)
【发布时间】:2021-06-20 03:21:29
【问题描述】:

heloo,我想创建一个带有列表图像的 pdf 文件。当我运行我的按钮代码时,我收到错误消息异常发生。 _TypeError(类型“Future”不是类型“Uint8List”的子类型)。

我的按钮:

ButtonWidget(
              text: 'Export PDF',
              onClicked: () async {
                final pdfFile = await ExportPdfApi.generateMyPDF();
                ExportPdfApi.openFile(pdfFile);
              },
            ),

函数获取图片:

static viewImage(image) async {
print("print $image");

final imageJpg1 = (await NetworkAssetBundle(Uri.parse(image)).load(image))
    .buffer
    .asUint8List();

print("print $imageJpg1");

return imageJpg1;
}

我的 pdf 中的列表视图:

ListView.builder(
          itemCount: _listAnggotaController.dataList.length,
          itemBuilder: (context, index) {
            return ClipRRect(
              horizontalRadius: 32,
              verticalRadius: 32,
              child: Image(
                MemoryImage(viewImage(
                    _listAnggotaController.dataList[index].idGambar)),
                height: 100,
              ),
            );
          }),

请帮助解决我的问题。 谢谢。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    首先Flutter在异步函数没有完成的时候会返回一个Future,完成后会返回type类型的数据。您犯的错误是从同步函数调用异步。尽管 dart 将异步执行函数(您可以通过从异步函数打印 imageJpg1 来验证这一点)。 但是你调用它的地方不是等待异步函数完成。

    我推荐使用 FutureBuilder

    我希望现在很清楚。 如需了解更多信息,请访问Asynchronous Programming

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2020-11-17
      • 2020-01-01
      • 2021-10-21
      • 2021-10-18
      • 1970-01-01
      • 2020-01-18
      相关资源
      最近更新 更多