【问题标题】:How can I show JSON response in my Snackbar?如何在我的 Snackbar 中显示 JSON 响应?
【发布时间】: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"

标签: json flutter api dart


【解决方案1】:

区分大小写。 "EncUserId" != "encUserId"

【讨论】:

  • 用户登录后我也可以在我的应用抽屉中显示这个吗?
  • like ,我怎样才能将此响应保存在变量中,以便我可以在我的应用程序的任何地方以Text() 形式使用它?
  • 可以,可以使用Hive本地数据库之类的本地存储,也可以在页面导航中发送by参数。
猜你喜欢
  • 1970-01-01
  • 2020-09-24
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多