Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Request 我如何称呼未来<;字符串>;在颤振中休息?_Request_Flutter - Fatal编程技术网

Request 我如何称呼未来<;字符串>;在颤振中休息?

Request 我如何称呼未来<;字符串>;在颤振中休息?,request,flutter,Request,Flutter,如何调用未来API?这是我的API代码 class ApiClient{ Future<String> getPasswordToken(String username, String password) async { var response = await http.post(Uri.parse(this.apiBaseUrl + "/auth/token"), headers: { "Accept": "application/jso

如何调用未来API?这是我的API代码

class ApiClient{

  Future<String> getPasswordToken(String username, String password) async {
    var response =
        await http.post(Uri.parse(this.apiBaseUrl + "/auth/token"), headers: {
      "Accept": "application/json"
    }, body: {
      "username": username,
      "password": password
    });

    if (response.statusCode == 200) {
      var token = OAuthToken.fromJson(json.jsonDecode(response.body));
      return token.accessToken;
    } else {
      throw Exception('Failed to fetch access token');
    }
  }
}
类ApiClient{
未来的getPasswordToken(字符串用户名、字符串密码)异步{
var响应=
等待http.post(Uri.parse(this.apiBaseUrl+“/auth/token”),标题:{
“接受”:“应用程序/json”
},主体:{
“用户名”:用户名,
“密码”:密码
});
如果(response.statusCode==200){
var token=OAuthToken.fromJson(json.jsonDecode(response.body));
返回token.accessToken;
}否则{
抛出异常(“无法获取访问令牌”);
}
}
}
我使用下面的容器来调用API。根据API响应需要导航另一个屏幕,我在按下按钮时点击了调用API

var apiClient = new ApiClient();
Container(
      margin: const EdgeInsets.only(top: 20.0),
      padding: const EdgeInsets.only(left: 20.0, right: 20.0),
      child: new Row(
        children: <Widget>[
          new Expanded(
            child: FlatButton(
              shape: new RoundedRectangleBorder(
                  borderRadius: new BorderRadius.circular(28.0)),
              splashColor: this.primaryColor,
              color: this.primaryColor,
              child: new Row(
                children: <Widget>[
                  new Padding(
                    padding: const EdgeInsets.only(left: 20.0),
                    child: Text(
                      "LOGIN",
                      style: TextStyle(color: Colors.white),
                    ),

                  ),

                 new Expanded(
                    child: Container(),
                  ),
                  new Transform.translate(
                    offset: Offset(15.0, 0.0),
                    child: new Container(
                      padding: const EdgeInsets.all(5.0),
                      child: FlatButton(
                        shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(28.0)),
                        splashColor: Colors.white,
                        color: Colors.white,
                        child: Icon(
                          Icons.arrow_forward,
                          color: this.primaryColor,
                        ),
                      onPressed: apiClient.getPasswordToken("",""),

                  /*        child:new FutureBuilder(future: apiClient.getClientToken(),
                            builder: (BuildContext context, AsyncSnapshot response) {
                              response.hasData==false? new SignIn(): new Scaffold(
                                appBar: new AppBar(title: new Text("Future Builder"),),
                                body: new Center(
                                  child: new Text("Build your widgets"),
                                ),
                              );
                            }
                        );*/
                      ),
                    ),
                  )
                ],
              ),
              onPressed: () => {},
              //onPressed: apiClient.getPasswordToken(emailInputField.key,emailInputField.key),
            ),
          ),
        ],
      ),
    );
var apiClient=new apiClient();
容器(
边距:仅限常量边集(顶部:20.0),
填充:仅限常量边集(左:20.0,右:20.0),
孩子:新的一排(
儿童:[
新扩展(
孩子:扁平按钮(
形状:新的RoundedRectangleBorder(
边界半径:新的边界半径。圆形(28.0)),
splashColor:this.primaryColor,
颜色:这个,原色,
孩子:新的一排(
儿童:[
新填料(
填充:仅限常量边集(左:20.0),
子:文本(
“登录”,
样式:TextStyle(颜色:Colors.white),
),
),
新扩展(
子级:容器(),
),
新Transform.translate(
偏移量:偏移量(15.0,0.0),
子容器:新容器(
填充:常数边集全部(5.0),
孩子:扁平按钮(
形状:新的RoundedRectangleBorder(
边界半径:新的边界半径。圆形(28.0)),
颜色:颜色。白色,
颜色:颜色,白色,
子:图标(
Icons.arrow_向前,
颜色:这个,原色,
),
onPressed:apiClient.getPasswordToken(“,”),
/*子级:新FutureBuilder(future:apiClient.getClientToken(),
生成器:(BuildContext上下文,异步快照响应){
response.hasData==false?new SignIn():new Scaffold(
appBar:new appBar(标题:新文本(“未来构建器”),),
正文:新中心(
子项:新文本(“构建您的小部件”),
),
);
}
);*/
),
),
)
],
),
按下:()=>{},
//按下:apiClient.getPasswordToken(emailInputField.key,emailInputField.key),
),
),
],
),
);

我试过上面的代码。无法将参数类型“Future”分配给参数类型“()→ void

这应该满足您的要求:

var apiClient = new ApiClient();
Container(
      margin: const EdgeInsets.only(top: 20.0),
      padding: const EdgeInsets.only(left: 20.0, right: 20.0),
      child: new Row(
        children: <Widget>[
          new Expanded(
            child: FlatButton(
              shape: new RoundedRectangleBorder(
                  borderRadius: new BorderRadius.circular(28.0)),
              splashColor: this.primaryColor,
              color: this.primaryColor,
              child: new Row(
                children: <Widget>[
                  new Padding(
                    padding: const EdgeInsets.only(left: 20.0),
                    child: Text(
                      "LOGIN",
                      style: TextStyle(color: Colors.white),
                    ),

                  ),

                 new Expanded(
                    child: Container(),
                  ),
                  new Transform.translate(
                    offset: Offset(15.0, 0.0),
                    child: new Container(
                      padding: const EdgeInsets.all(5.0),
                      child: FlatButton(
                        shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(28.0)),
                        splashColor: Colors.white,
                        color: Colors.white,
                        child: Icon(
                          Icons.arrow_forward,
                          color: this.primaryColor,
                        ),
                      onPressed: apiClient.getPasswordToken("",""),

                  /*        child:new FutureBuilder(future: apiClient.getClientToken(),
                            builder: (BuildContext context, AsyncSnapshot response) {
                              response.hasData==false? new SignIn(): new Scaffold(
                                appBar: new AppBar(title: new Text("Future Builder"),),
                                body: new Center(
                                  child: new Text("Build your widgets"),
                                ),
                              );
                            }
                        );*/
                      ),
                    ),
                  )
                ],
              ),
              onPressed: () => {},
              //onPressed: apiClient.getPasswordToken(emailInputField.key,emailInputField.key),
            ),
          ),
        ],
      ),
    );
onPressed: () => apiClient.getPasswordToken("",""),


你的电话线在哪里

onPressed: apiClient.getPasswordToken("",""),
您正在将Future(这是getPasswordToken的返回值)传递给按下的,它需要一个函数

你应该这样做

onPressed: () => apiClient.getPasswordToken("",""),


谢谢。在得到响应代码200后,如何导航到下一页?当结果不是
200
时,您的
getPasswordToken
会引发异常,您只需将代码包装在
onPressed:()async{try{wait getPasswordToken();Navigator.of(context)…}on exception catch(e){…}
使用其中一种方法,该方法具有
replace
push
remove
,以从历史堆栈中获取登录页面,并将其替换为另一个。
onPressed: () {
  apiClient.getPasswordToken("","");
}