flutter - 将 API 返回的错误消息显示到 Flutter UI
问题描述
将 API 返回的错误消息显示到 Flutter UI 中。
我是 Flutter 的新手。我的 Laravel API 返回以下响应,我已成功接收响应,但我无法找到在 UI 中显示这些消息的方法,我正在使用 Flushbar 显示消息。请帮忙。
{
"data":{
"errors": {
"email": [
"The email has already been taken."
],
"password": [
"The password must be at least 6 characters.",
"The password confirmation does not match."
]
}
}
}
解决方案
var res = {
"data":{
"errors": {
"email": [
"The email has already been taken."
],
"password": [
"The password must be at least 6 characters.",
"The password confirmation does not match."
]
}
}
};
res["data"]["errors"].forEach((key, messages) {
if ("email" == key) {
// show email errors like this
for (var message in messages) {
// Use your Flushbar here to show the error message
}
} else if ("password" == key) {
// show password erros like this
for (var message in messages) {
// Use your Flushbar here to show the error message
}
}
});
编辑
为了更短,您可以像这样将您的消息合并为一个
String combinedMessage = "";
res["data"]["errors"].forEach((key, messages) {
for (var message in messages) combinedMessage = combinedMessage + "- $message\n";
// Use your Flushbar here to show combinedMessage variable
});
推荐阅读
- sql-server - 自动添加的主键尝试插入 NULL 的值
- javascript - Appmaker Typeerror 建议 _equals 方法是我无法调用的属性
- android - 如何在模拟器中连接自定义wifi网络?
- google-apps-script - 尝试使用 GmailApp.sendEmail - 获取缺少的访问令牌进行授权
- sql - 选择带有修改值和通配符的语句
- python - Matplotlib 在 3d 表面上绘制轮廓
- algorithm - 阿姆斯壮数逻辑
- swift - 将对象存储在结构中时出现 Swift 问题
- javascript - 具有值的行的重复功能
- sql - 使用闪回查询时 BST 到 GMT Oracle 错误 (ORA-08186)