Line data Source code
1 : /// Represents a generic API response. 2 : /// 3 : /// The [ApiResponse] class encapsulates the details of an HTTP response, including status code, headers, body, exception message, and success indicator. 4 : /// 5 : /// ### Type Parameters 6 : /// - [T]: The type of the response body. 7 : /// 8 : /// ### Properties 9 : /// - [statusCode]: HTTP status code of the response. 10 : /// - [headers]: HTTP headers returned by the server. 11 : /// - [body]: The response body of type [T]. 12 : /// - [exception]: Exception message if an error occurred. 13 : /// - [isSuccess]: Indicates whether the response is successful. 14 : /// 15 : /// ### Constructors 16 : /// - [ApiResponse]: Creates an instance with the provided values. 17 : /// 18 : /// ### Methods 19 : /// - [copyWith]: Returns a copy of the instance with modified values. 20 : /// 21 : /// ### Example 22 : /// ```dart 23 : /// final response = ApiResponse<String>( 24 : /// statusCode: 200, 25 : /// headers: {'Content-Type': 'application/json'}, 26 : /// body: '{"result":true}', 27 : /// exception: '', 28 : /// isSuccess: true, 29 : /// ); 30 : /// print(response.statusCode); // 200 31 : /// print(response.body); // '{"result":true}' 32 : /// ``` 33 : class ApiResponse<T> { 34 1 : const ApiResponse({ 35 : required this.statusCode, 36 : required this.headers, 37 : required this.body, 38 : required this.exception, 39 : required this.isSuccess, 40 : }); 41 : 42 : /// HTTP status code of the response. 43 : final int statusCode; 44 : 45 : /// HTTP headers returned by the server. 46 : final Map<String, String> headers; 47 : 48 : /// The response body of type [T]. 49 : final T body; 50 : 51 : /// Exception message if an error occurred. 52 : final String exception; 53 : 54 : /// Indicates whether the response is successful. 55 : final bool isSuccess; 56 : 57 : /// Returns a copy of the instance with modified values. 58 : /// 59 : /// [body]: New response body (optional). 60 : /// [headers]: New headers (optional). 61 : /// [statusCode]: New status code (optional). 62 : /// [exception]: New exception message (optional). 63 : /// [isSuccess]: New success indicator (optional). 64 1 : ApiResponse copyWith({ 65 : dynamic body, 66 : Map<String, String>? headers, 67 : int? statusCode, 68 : String? exception, 69 : bool? isSuccess, 70 : }) { 71 1 : return ApiResponse( 72 1 : statusCode: statusCode ?? this.statusCode, 73 1 : headers: headers ?? this.headers, 74 1 : body: body ?? this.body, 75 1 : exception: exception ?? this.exception, 76 1 : isSuccess: isSuccess ?? this.isSuccess, 77 : ); 78 : } 79 : }