dart - 如何修复 future<int> 类型的值不能分配给 int 类型的变量

标签 dart flutter hybrid-mobile-app mobile-application

我正在尝试使用 flutter 框架在数据库中的表中插入一行,当我将返回的值分配给新变量时,我收到以下错误消息

 a value of type future<int> can not be assigned to a variable of type int

这是插入新行的函数

Future<int> addContact(String contact_name, String contact_phone) async {
    Database c = await getConnection;
    Map<String, dynamic> contact = getMap(contact_name, contact_phone);
    return await c.insert("contacts", contact);
  }

这里是我获取返回数据的地方

int result=db.addContact(name, phone);

最佳答案

addContact函数有一些操作,操作完成后会返回值,所以在这里做一个完成 block .....

addContact(contact_name, contact_phone).then((value) {
  //this 'value' is in int
  //This is just like a completion block
});

关于dart - 如何修复 future<int> 类型的值不能分配给 int 类型的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52756388/

相关文章:

dart - 从 future 实例中获取值(value)

ios - 如何使用 faSTLane 缩短 Flutter iOS 应用程序的构建时间

android - 如何在 flutter 中禁用 RadioListTile 小部件

html - Paypal 混合应用

angularjs - 单击 ionic 中的链接时如何使网页在应用程序内打开?

list - Dart , flutter :范围索引

flutter - Dart 中字符串的默认值是多少?

database - 将DateTime对象上传到NoSQL数据库时出错

android - flutter 的android应用程序在 Release模式下卡住在启动屏幕上

java - 如何添加到 cordova 中的 settings.gradle