Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何确定future类型的值<;int>;无法分配给int类型的变量_Dart_Flutter_Hybrid Mobile App_Mobile Application - Fatal编程技术网

Dart 如何确定future类型的值<;int>;无法分配给int类型的变量

Dart 如何确定future类型的值<;int>;无法分配给int类型的变量,dart,flutter,hybrid-mobile-app,mobile-application,Dart,Flutter,Hybrid Mobile App,Mobile Application,我正试图使用flatterframework向数据库中的表插入一行,当我将返回值赋给一个新变量时,我得到以下错误消息 a value of type future<int> can not be assigned to a variable of type int 您可以使用FutureBuilder。以下是一个例子: @override Widget build(BuildContext context) { return new FutureBuilder (

我正试图使用flatterframework向数据库中的表插入一行,当我将返回值赋给一个新变量时,我得到以下错误消息

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

您可以使用
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();
          }
        }
    );  
}

使用
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 ....

addContact函数有一些操作,当操作完成时会返回值,所以在这里创建一个完成块

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

可能
int result=wait db.addContact(姓名、电话)?@alexkucksdorf不起作用,因为awaiat需要异步的方法,并且您不能修改SativeWidget classAsync的构建函数,因为它具有传染性。无法从异步返回到同步。调用方还需要
async
关键字,并使用
wait
依赖于异步代码在继续执行之前完成。然后,您应该使用
FutureBuilter
@GünterZöchbauer。您能提供一个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 ....
addContact(contact_name, contact_phone).then((value) {
  //this 'value' is in int
  //This is just like a completion block
});