BaseResponse<T> class abstract

Abstract base class for all API response models in the SDK.

This class provides a standardized structure for handling API responses across the entire SDK. It encapsulates common response fields like success status, response codes, messages, and data payloads.

Features

  • Standardized Structure: Consistent response format across all APIs
  • Generic Data Support: Type-safe data payload handling
  • Error Information: Built-in support for error messages and codes
  • Success Tracking: Boolean flag for quick success/failure checks

Usage Example

class PaymentResponse extends BaseResponse&lt;PaymentData&gt; {
  const PaymentResponse({
    required super.success,
    super.responseCode,
    super.message,
    super.data,
  });

  factory PaymentResponse.fromJson(Map&lt;String, dynamic&gt; json) {
    return PaymentResponse(
      success: json['success'] ?? false,
      responseCode: json['responseCode'],
      message: json['message'],
      data: json['data'] != null
          ? PaymentData.fromJson(json['data'])
          : null,
    );
  }
}

// Usage
final response = PaymentResponse.fromJson(jsonData);
if (response.success) {
  final paymentData = response.data!;
  print('Payment processed: ${paymentData.transactionId}');
} else {
  print('Payment failed: ${response.message}');
}

Generic Type Parameter

  • T: The type of data contained in the response
  • Can be any type: primitive types, custom models, or collections
  • Use void or dynamic for responses that don't contain data

Response Structure

All API responses follow this structure:

{
  "success": true,
  "responseCode": 200,
  "message": "Operation completed successfully",
  "data": { /* response-specific data */ }
}

Field Descriptions

  • success: Indicates whether the operation was successful
  • responseCode: HTTP status code or custom response code
  • message: Human-readable message describing the result
  • data: The actual response payload (type depends on the API)

Best Practices

When extending this class:

  • Always implement a fromJson factory constructor
  • Handle null data gracefully
  • Provide meaningful default values where appropriate
  • Include proper error handling for malformed responses
  • Use the generic type parameter for type safety
Implementers

Constructors

BaseResponse.new({required bool success, int? responseCode, String? message, T? data})
Creates a new BaseResponse instance.
const

Properties

data → T?
The actual data payload of the response.
final
hashCode int
The hash code for this object.
no setterinherited
message String?
Human-readable message describing the response result.
final
responseCode int?
The response code associated with this response.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
success bool
Indicates whether the API operation was successful.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited