How can i call the Future<string> rest api in flutter?
825
This should do what you want:
onPressed: () => apiClient.getPasswordToken("",""),
or
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
}
Author by
sekhar
Updated on December 06, 2022Comments
-
sekhar over 1 year
How can I call the Future API? This is my API code
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'); } } }
And am using the below container to call the API. Based on the API response need to navigate another screen I have struck the calling API in onPressed
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), ), ), ], ), );
I tried the above code. it showing The argument type 'Future' can't be assigned to the parameter type '() → void
-
sekhar over 5 yearsThank u. How can navigate to next page after getting the response code 200?
-
Günter Zöchbauer over 5 yearsYour
getPasswordToken
throws an exception when the result is not200
, you you can just wrap the code in theonPressed: () async { try { await getPasswordToken(); Navigator.of(context)....} on Exception catch(e) { ...}
with one of the methods that hasreplace
orpush
andremove
in them docs.flutter.io/flutter/widgets/Navigator-class.html to get the login page from the history stack and replace it by a different one.