【发布时间】:2021-10-16 02:24:22
【问题描述】:
登录后,我成功地从我的 api 获得了响应,也可以通过这样做 Text("Status: ${widget.rresponse.status}"), 在我的第二个屏幕中打印数据。
但我想在小吃店打印。
在我的 SnackBar 中打印 json 响应数据的语法是什么?
这是我的 Json 响应
{
"Status": "1",
"Message": "You are Logged in successfully",
"UserData": {
"Name": "qwerty@gmail.com",
"EncUserId": "GO9gj3aSUKCpxE3AMSbh/A=="
}
}
当用户按下登录按钮时,我的 Api 被调用
Future<void> login() async{
var jsonResponse;
if (passwordontroller.text.isNotEmpty && emailController.text.isNotEmpty) {
var response = await http.post(Uri.parse("http://ututututtutu"),
body: ({
'LoginId': emailController.text,
'Password': passwordontroller.text
}));
if (response.statusCode == 200) {
print("Correct");
print(response.body);
jsonResponse = json.decode(response.body.toString());
print(jsonResponse);
// ===========Successfully can print in my second screen ==================
// Navigator.push(context, MaterialPageRoute(builder: (context)=>AfterLoginResPage(rresponse: ApiResponse.fromJson(jsonResponse))));
//===========Trying to print in my snackBar========================
jsonResponse = jsonDecode(response.body.toString());//==
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content:Text("Message: ${jsonResponse['encUserId']}"))) ;
}
else {
print("Wronggooooooooooooooooooooooooooo");
print(response.body);
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Invalid credentials")));
}
} else {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Blank field is not allowed")));
}
}
这是我的Model 课程
class ApiResponse {
ApiResponse({
required this.status,
required this.message,
required this.userData,
});
String status;
String message;
UserData? userData;
factory ApiResponse.fromJson(Map<String, dynamic> json) => ApiResponse(
status: json["Status"],
message: json["Message"],
userData: json["UserData"] == null? null:UserData.fromJson(json["UserData"] as Map<String, dynamic>),
);
}
class UserData {
UserData({
required this.name,
required this.encUserId,
});
String name;
String encUserId;
factory UserData.fromJson(Map<String, dynamic> json) => UserData(
name: json["Name"],
encUserId: json["EncUserId"],
);
【问题讨论】:
-
区分大小写。 "EncUserId" != "encUserId"