所以我使用rest api作为后端,我无法编辑,所以我发现需要传递令牌,从登录到此更改配置文件api,以便用户可以成功编辑他或她的详细信息。
Future updateProfile() 异步 { 字符串 url = "http://167.71.232.83:8000/myprofile/${userId}/";
// Build the query parameters with updated user information
Map<String, String> queryParams = {
"email": _emailController.text,
"password": passwordController.text,
"repassword": repasswordController.text,
"firstname": firstname.text,
"lastname": lastname.text,
"phonenumber": phonenumber.text,
"state": state.text,
"city": city.text,
"Zip": zip.text,
"mailing": _value.toString(),
"referred_by": selectedItem,
"flag": "business",
"businessname": businesscontroller.text,
};
String queryString = Uri(queryParameters: queryParams).query;
url += '?' + queryString;
try {
final response = await http.put(
Uri.parse(url),
headers: {
"Content-Type": "application/json",
"User-Agent": "PostmanRuntime/7.28.4",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
},
);
var data = jsonDecode(response.body);
print(url);
print(response.statusCode);
print(data);
if (response.statusCode == 200) {
// Success
_showSuccessDialog();
// Navigate to the next page or perform any other actions
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => BottomPage(),
),
);
} else {
// Error
_showErrorDialog('Failed to update profile. Please try again.');
}
print(response.body);
} catch (e) {
print('Error updating profile: $e');
_showErrorDialog('Failed to update profile. Please try again.');
}
}
我附上了我尝试过的上面的代码片段 我需要修改它,使其可以从成功登录中访问令牌,这可以帮助编辑信息。
这应该有帮助
final token = "your-backend-token";
final response = await http.put(
Uri.parse(url),
headers: {
"Content-Type": "application/json",
"User-Agent": "PostmanRuntime/7.28.4",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
HttpHeaders.authorization: token,
},
);
另外你也可以有这样的东西
class ApiHandler {
ApiHandler.instance(); // for instantiating request withou token
String? _token;
ApiHandler.tokenInstance() {
_token = getToken() ?? throw "No Token found";
// `getToken()` gets token from shared preferences or wherever you have stored.
}
// use this for public api requests
Map<String, String> get jsonHeaders = {
HttpHeaders.contentType: "application/json",
}
// use this for authorized api requests
Map<String, String> get tokenHeaders = {
HttpHeaders.contentType: "application/json",
HttpHeaders.authorization: _token,
}
}