简体   繁体   English

“未来”的实例 <dynamic> &#39;在Flutter

[英]Instance of 'Future<dynamic>' in Flutter

I'm getting an int Id from server which in HTTP request in flutter, it is OK and I want to save this value in an variable , but when i call the returned value i get this error Instance of 'Future' , any suggestion ?? 我从服务器获取一个int Id,在HTTP请求中,它可以在一个变量中保存这个值,但是当我调用返回值时,我得到这个错误“未来”的实例,有什么建议吗? ? Here is the code 这是代码

                    child: RaisedButton(onPressed:() {
                    CurrentPrjId= SaveProject();
                    print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
                    print(CurrentPrjId);
                    Navigator.of(context).pushReplacementNamed('/AllUSerStories');
                }
//Save Project

  Future SaveProject() async {

//SaveProjectResult has the id of the current created project which i need that for its user stories
var SaveProjectResult = await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
print(":::::::::::::::::::::::::::::::The Project Is Created And Here Its ID In SaveProject:::::::::");
print(SaveProjectResult);
return SaveProjectResult;

} }

Without having your code, it's hard to guess, whats going on.. But you may try to wait for the completion of the future.. so do this: 没有你的代码,很难猜测,发生了什么......但你可能会试着等待未来的完成..所以这样做:

methOdThatLoadsId().then((id) => print("Id that was loaded: $id"));

or 要么

var id = await methodThatLoadsId();

Good luck :) 祝好运 :)

sorry for the late reply. 这么晚才回复很抱歉。 You've added the await at the wrong place unfortunately. 不幸的是,你已经在错误的地方添加了await。 Try something like this: 尝试这样的事情:

                   child: RaisedButton(onPressed:() async {
                    CurrentPrjId=  await SaveProject();
                    print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
                    print(CurrentPrjId);
                    Navigator.of(context).pushReplacementNamed('/AllUSerStories');
                }
//Save Project

  Future SaveProject() async {

//SaveProjectResult has the id of the current created project which i need that for its user stories
var SaveProjectResult = await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
print(":::::::::::::::::::::::::::::::The Project Is Created And Here Its ID In SaveProject:::::::::");
print(SaveProjectResult);
return SaveProjectResult;

Or shorter: 或更短:

                   child: RaisedButton(onPressed:() async {
                    CurrentPrjId=  await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
                    print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
                    print(CurrentPrjId);
                    Navigator.of(context).pushReplacementNamed('/AllUSerStories');
                }

You have to use a FutureBuilder widget to get Future value from an async call. 您必须使用FutureBuilder小部件从异步调用中获取Future值。

CurrentPrjId=FutureBuilder<String>(
    future: SaveProject(),
    builder: (context, snapshot) {
      if (snapshot.hasData) return Text(snapshot.data);
      else if (snapshot.hasError) return Text(snapshot.error);
      return return Text("Await for data");
    },
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM