【问题标题】:Instance of 'Future<dynamic>' in FlutterFlutter 中“Future<dynamic>”的实例
【发布时间】:2019-09-08 17:22:47
【问题描述】:

我从服务器获取一个 int Id,它在 Flutter 中的 HTTP 请求中,没关系,我想将此值保存在一个变量中,但是当我调用返回值时,我得到了这个错误 'Future' 实例,有什么建议吗?? 这是代码

                    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;

}

【问题讨论】:

  • 请出示您的代码
  • 嗨,我添加了代码

标签: c# asp.net-mvc dart flutter future


【解决方案1】:

如果没有你的代码,很难猜测,发生了什么..但你可以尝试等待未来的完成..这样做:

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

var id = await methodThatLoadsId();

祝你好运:)

【讨论】:

  • 我使用了您的第一个解决方案,但仍然遇到同样的问题:“未来”实例
  • 我们如何在initState函数中使用await?谢谢。
  • @Kamlesh 据我所知,这根本不可能。尽管如此,有几种方法可以在 initState 中使用异步任务。请参阅stackoverflow.com/a/51901192/11266880 了解一种解决方案
【解决方案2】:

抱歉回复晚了。 不幸的是,您在错误的地方添加了等待。 试试这样的:

                   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;

或更短:

                   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');
                }

【讨论】:

    【解决方案3】:

    您必须使用 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");
        },
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 2023-02-11
      • 2020-09-03
      • 1970-01-01
      • 2018-12-01
      • 2018-09-28
      • 1970-01-01
      相关资源
      最近更新 更多