【问题标题】:Why does await doesn't extract values from a Future in flutter? [closed]为什么 await 不会从 Flutter 中的 Future 中提取值? [关闭]
【发布时间】:2020-06-25 19:21:25
【问题描述】:

我正在尝试创建一个 sqflite 数据库,所以我在这里按照 Flutter 官方网站上的教程进行操作:(https://flutter.dev/docs/cookbook/persistence/sqlite),我从教程中复制并粘贴了相同的代码,但它不起作用。我立即得到错误。

当然,我包含了代码运行所需的所有包:code photo

这些是我在await getDatabasesPath() 上遇到的错误:

Unexpected text 'await'.Try removing the text
The argument type 'Future<String>' can't be assigned to the parameter type 'String'

【问题讨论】:

  • 请确保在编辑器中发布具有所提供功能的代码。它可以帮助其他用户调查问题。
  • 据我所知,您正在初始化一个字段/属性。如果在异步方法中调用,则只能使用 await。您的代码中不是这种情况。为了帮助您编写有用的代码,我建议您提供更多上下文。
  • 你不能在非异步函数中使用 await。

标签: asynchronous flutter dart async-await sqflite


【解决方案1】:

请务必将async 添加到您的方法签名中,例如:

void myFunc async { 
  await future();
}

【讨论】:

  • 谢谢。正如你所说,我必须将那段代码封装在一个函数中。非常感谢
猜你喜欢
  • 2021-04-08
  • 2020-08-04
  • 2016-05-12
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 2023-03-17
  • 2019-08-31
相关资源
最近更新 更多