Skip to content

Instantly share code, notes, and snippets.

@shamrat1
Created January 31, 2022 09:27
Show Gist options
  • Save shamrat1/0277c73649cdadda4594b1ad82bce8cd to your computer and use it in GitHub Desktop.
Save shamrat1/0277c73649cdadda4594b1ad82bce8cd to your computer and use it in GitHub Desktop.
Handle Laravel specific validation error Response Model
// To parse this JSON data, do
//
// final errorResponse = errorResponseFromJson(jsonString);
import 'dart:convert';
ErrorResponse errorResponseFromJson(String str) =>
ErrorResponse.fromJson(json.decode(str));
String errorResponseToJson(ErrorResponse data) => json.encode(data.toJson());
class ErrorResponse {
ErrorResponse({
this.message,
this.errors,
});
String? message;
Map<String, String>? errors;
factory ErrorResponse.fromJson(Map<String, dynamic> json) {
Map<String, String> errors = {};
if (json["errors"] != null) {
json["errors"].forEach((key, value) {
errors[key] = value[0];
});
}
return ErrorResponse(
message: json["message"],
errors: errors,
);
}
Map<String, dynamic> toJson() => {
"message": message,
"errors": errors,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment