Created
January 31, 2022 09:27
-
-
Save shamrat1/0277c73649cdadda4594b1ad82bce8cd to your computer and use it in GitHub Desktop.
Handle Laravel specific validation error Response Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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