'Rest API in Flutter Login
When I give header and body information inside my http request, it redirects automatically to next page even if i dont give any login credential in my Mobile App.
Future<Login> fetchLoginData() async {
final http.Response response = await http.post(
'http://lmsapi.design.net:88//api/login/login',
headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic XfnatLYBaDO2AKP6KfcIJg=='
},
/* body: {
'companycode': 'ABC1001',
'deploymentcode': 'ui11'
}*/
);
if (response.statusCode == 200) {
// then parse the JSON.
return Login.fromJson(jsonDecode(response.body));
} else {
// then throw an exception.
throw Exception('Failed to load Data');
}
}
Can someone help me how to pass header and body inside my http request?
Solution 1:[1]
The issue doesn't seem to be about handling the http response and leans more toward on how the navigation to the next page is handled. You'd need to check if fetchLoginData() returns the response. Future callback can be handled by using fetchLoginData().then() for example.
fetchLoginData().then((Login? loginData){
if(loginData!=null){
// Handle Navigation
}
});
Then you may consider implementing the navigation once you got a successful response from fetchLoginData()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Omatt |
