Line data Source code
1 : import 'dart:convert'; 2 : 3 : /// Represents a successful API response. 4 : /// 5 : /// The [ApiSuccess] class encapsulates the data of a successful response, including the HTTP status code and the returned data. 6 : /// 7 : /// ### Properties 8 : /// - [code]: HTTP status code of the response. 9 : /// - [data]: Data returned by the API (usually JSON or plain text). 10 : /// 11 : /// ### Constructors 12 : /// - [ApiSuccess]: Creates an instance with the provided values. 13 : /// - [ApiSuccess.fromJson]: Creates an instance from a JSON string. 14 : /// - [ApiSuccess.fromMap]: Creates an instance from a Map. 15 : /// - [ApiSuccess.empty]: Creates an empty instance (code=0, data=''). 16 : /// 17 : /// ### Methods 18 : /// - [copyWith]: Returns a copy of the instance with modified values. 19 : /// 20 : /// ### Example 21 : /// ```dart 22 : /// final success = ApiSuccess(code: 200, data: '{"result":true}'); 23 : /// print(success.code); // 200 24 : /// print(success.data); // '{"result":true}' 25 : /// ``` 26 : class ApiSuccess { 27 : /// HTTP status code of the response. 28 : final int code; 29 : 30 : /// Data returned by the API (usually JSON or plain text). 31 : final String data; 32 : 33 : /// Main constructor. 34 : /// 35 : /// [code]: HTTP status code. 36 : /// [data]: Data returned by the API. 37 2 : ApiSuccess({required this.code, required this.data}); 38 : 39 : /// Returns a copy of the instance with modified values. 40 : /// 41 : /// [code]: New status code (optional). 42 : /// [data]: New data (optional). 43 : /// [message]: Not used, only for compatibility. 44 1 : ApiSuccess copyWith({int? code, String? data, String? message}) => 45 3 : ApiSuccess(code: code ?? this.code, data: data ?? this.data); 46 : 47 : /// Creates an instance from a JSON string. 48 : /// 49 : /// [str]: JSON string representing the response. 50 1 : factory ApiSuccess.fromJson(String str) => 51 2 : ApiSuccess.fromMap(json.decode(str)); 52 : 53 : /// Creates an instance from a Map. 54 : /// 55 : /// [json]: Map with the response data. 56 1 : factory ApiSuccess.fromMap(Map<String, dynamic> json) => 57 3 : ApiSuccess(code: json["code"] ?? 0, data: json["data"] ?? ''); 58 : 59 : /// Creates an empty instance (code=0, data=''). 60 2 : factory ApiSuccess.empty() => ApiSuccess(code: 0, data: ''); 61 : }