登錄后端回應令牌后。現在如何獲得那個令牌?
我在這個專案中使用了 HTTP 包。我是撲撲的新手。我的代碼示例:
Future<void> login() async {
if (_password.text.isNotEmpty && _number.text.isNotEmpty) {
final response = await http.post(
Uri.parse(
'https://api.azabazar.com/api/login/?fbclid=IwAR2Sz4ky31HCG4g2Sbhn08LV3QqV76YEaIIQwpRavXRB1A4o0fq4aiQ22kE'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
HttpHeaders.authorizationHeader: 'Basic your_api_token_here', //here i want my token
},
body: jsonEncode(<String, String>{
"username": _number.text,
"password": _password.text,
}),
);
if (response.statusCode == 201) {
const Text("Loged in");
} else {
throw Exception('Failed to find account.');
}
} else {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("invalid form")));
}
}
uj5u.com熱心網友回復:
我假設你從服務器得到了這樣的東西:
{
"data": {
"token": "TOKEN"
...
}
}
您可以像這樣獲取令牌:
if (response.statusCode == 201) {
final decodedResponse = jsonDecode(response.body) as Map;
final token = decodedResponse['data']['token'];
} else {
throw Exception('Failed to find account.');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/372645.html
