如何解决未来类型的值 不能被分配给一个int类型的可变

问题描述 投票:1回答:3

我试图用扑框架插入一行到数据库中的表,当我指派返回值到一个新的变量,我得到了以下错误消息

 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);
dart flutter hybrid-mobile-app mobile-application
3个回答
1
投票

您可以使用FutureBuilder。下面是一个例子:

@override  
Widget build(BuildContext context) {
    return new FutureBuilder (
        future: loadFuture(), //This is the method that returns your Future
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data) {               
              return customBuild(context);  //Do stuff and build your screen from this method
            }
          } else {
            //While the future is loading, show a progressIndicator
            return new CustomProgressIndicator();
          }
        }
    );  
}

0
投票

要么使用FutureBuilder或类似这样的东西

int value; //should be state variable. As we want to refresh the view after we get data

@override  
Widget build(BuildContext context) {
  db.addContact(name, phone).then((value) {
       setState(() {this.value = value;});
  })
 return ....

0
投票

的addContact功能有一定的操作将返回当操作完成值,所以要完成块这里.....

addContact(contact_name, contact_phone).then((value) {
  //this 'value' is in int
  //This is just like a completion block
});
© www.soinside.com 2019 - 2024. All rights reserved.