Reputation: 103
I have created a login feature using Postman token bearer which allows users to stay logged in even though the application has been closed. And now I want to edit the email and profile password that has been logged into the application and then save it with the same token bearer from the saved sharpref.
How to implement it? Below is my code to get the data but this code still has an error:
getPerfs() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var apikey = prefs.getString('apikey');
print(apikey);
}
var url = 'http://api.batulimee.com/v1_ship/my_profile';
Future<Map> getData() async {
var fullUrl = url + await getPerfs();
var response = await http.post(fullUrl);
return json.decode(response.body) as Map<String, dynamic>;
}
and this is json structure from the API url my_profile.
{
"status": "success",
"data": {
"id_user": 49,
"id_role": "8",
"name_role": "Ship Owner",
"first_name": "a",
"last_name": "f",
"email": "[email protected]",
"phone": "082258785595",
"saldo": "0",
"company_name": "aa",
"company_address": "jl kav pgri",
"photo": "https://batulimee.com/foto_user/avatar.png"
},
"message": "get profile detail successfully "
}
Which if you want to get it, you have to use the Postman authorization token bearer.
How to get all of this data and edit it?
Upvotes: 0
Views: 1496
Reputation: 93
The first thing is in your getPerfs() method, I can see you are saving apikey but that is not accessible to other methods which getData() in your case.
I would do this bit differently,
Future<Map<String,dynamic>> getData() async {
try {
var url = 'http://api.batulimee.com/v1_ship/my_profile';
SharedPreferences prefs = await SharedPreferences.getInstance();
var apikey = prefs.getString('apikey');
var response = await http.post(
url,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Bearer Token" : apikey
},
);
var checkResponse = json.decode(response.body) as Map<String, dynamic>;
print('checkResponse');
return checkResponse;
} catch (e) {
print(e);
throw(e);
}
}
Just copy and paste this code and you will get your data, but I would recommend you to save your fetched data in a variable instead of returning data.
One more thing, this could be a get API instead of post API because you are giving nothing in the request payload.
Upvotes: 1