代码之家  ›  专栏  ›  技术社区  ›  sekhar

如何在flatter中调用Future<string>restapi?

  •  1
  • sekhar  · 技术社区  · 6 年前

    我怎么能叫警察 未来

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

    我使用下面的容器来调用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),
                ),
              ),
            ],
          ),
        );
    

    我试过上面的代码。它正在显现 参数类型“Future”无法分配给参数类型“()void”

    2 回复  |  直到 6 年前
        1
  •  1
  •   Günter Zöchbauer    6 年前

    这应该是你想要的:

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

    onPressed: () async {
      var result = await apiClient.getPasswordToken("","");
      print(result);
      setState(() => this.foo = result); // you can omit `this.` it's just for demonstration purposes that this line would modify the widgets state
    }
    
        2
  •  0
  •   rmtmckenzie    6 年前

    你的队伍

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

    您正在传递一个Future(这是getPasswordToken的返回值) onPressed ,它需要一个函数。

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

    onPressed: () {
      apiClient.getPasswordToken("","");
    }