Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Flutter Generic API Response Class Dynamic Class Data Type

My app currently is working with a custom classes for each API responses as models. But I'm trying to change it, to optimize some little things, so I'm trying to implement a Class wrapper, called ApiResponse for example. But its not working fine the static call and methods, for make fromJson and toJson.

I will show what I'm trying, as example.

MyModel -> class response.
ApiResponse -> main class that contains any model class inside, and must be call child methods as itselfs 'fromjson/tojson'.
Test -> class for test purpose, errors comments on classes.

class MyModel {
  String id;
  String title;
  MyModel({this.id, this.title});

  factory MyModel.fromJson(Map<String, dynamic> json) {
    return MyModel(
      id: json["id"],
      title: json["title"],
    );
  }

  Map<String, dynamic> toJson() => {
        "id": this.id,
        "title": this.title,
      };
}

class ApiResponse<T> {
  bool status;
  String message;
  T data;
  ApiResponse({this.status, this.message, this.data});

  factory ApiResponse.fromJson(Map<String, dynamic> json) {
    return ApiResponse<T>(
        status: json["status"],
        message: json["message"],
        data: (T).fromJson(json["data"])); // The method 'fromJson' isn't defined for the type 'Type'.
                                           // Try correcting the name to the name of an existing method, or defining a method named 'fromJson'.
  }

  Map<String, dynamic> toJson() => {
        "status": this.status,
        "message": this.message,
        "data": this.data.toJson(), // The method 'toJson' isn't defined for the type 'Object'.
                                    // Try correcting the name to the name of an existing method, or defining a method named 'toJson'
      };
}

class Test {
  test() {
    ApiResponse apiResponse = ApiResponse<MyModel>();
    var json = apiResponse.toJson();
    var response = ApiResponse<MyModel>.fromJson(json);
  }
}
like image 597
Alberto Acuña Avatar asked Aug 26 '26 09:08

Alberto Acuña


2 Answers

You can't call methods on types on Dart because static methods must be resolved at compile time and types do not have a value until runtime.

You can, however, pass a parser callback to your constructor and use an interface(eg. Serializable) that every model may implement. Then, by updating your ApiResponse to ApiResponse<T extends Serializable> it will know that every type T will have a toJson() method.

Here's the full example updated.

class MyModel implements Serializable {
  String id;
  String title;
  MyModel({this.id, this.title});

  factory MyModel.fromJson(Map<String, dynamic> json) {
    return MyModel(
      id: json["id"],
      title: json["title"],
    );
  }

  @override
  Map<String, dynamic> toJson() => {
        "id": this.id,
        "title": this.title,
      };
}

class ApiResponse<T extends Serializable> {
  bool status;
  String message;
  T data;
  ApiResponse({this.status, this.message, this.data});

  factory ApiResponse.fromJson(Map<String, dynamic> json, Function(Map<String, dynamic>) create) {
      return ApiResponse<T>(
      status: json["status"],
      message: json["message"],
      data: create(json["data"]),
    );
  }

  Map<String, dynamic> toJson() => {
        "status": this.status,
        "message": this.message,
        "data": this.data.toJson(),
      };
}

abstract class Serializable {
  Map<String, dynamic> toJson();
}

class Test {
  test() {
    ApiResponse apiResponse = ApiResponse<MyModel>();
    var json = apiResponse.toJson();
    var response = ApiResponse<MyModel>.fromJson(json, (data) => MyModel.fromJson(data));
  }
}
like image 119
Miguel Ruivo Avatar answered Aug 27 '26 22:08

Miguel Ruivo


base_response.dart

class BaseResponse {
  dynamic message;
  bool success;


  BaseResponse(
      {this.message, this.success});

  factory BaseResponse.fromJson(Map<String, dynamic> json) {
    return BaseResponse(
        success: json["success"],
        message: json["message"]);
  }
}

list_response.dart

server response for list
{
  "data": []
  "message": null,
  "success": true,
}

@JsonSerializable(genericArgumentFactories: true)
class ListResponse<T> extends BaseResponse {
  List<T> data;

  ListResponse({
    String message,
    bool success,
    this.data,
  }) : super(message: message, success: success);

  factory ListResponse.fromJson(Map<String, dynamic> json, Function(Map<String, dynamic>) create) {
    var data = List<T>();
    json['data'].forEach((v) {
      data.add(create(v));
    });

    return ListResponse<T>(
        success: json["success"],
        message: json["message"],
        data: data);
  }
}

single_response.dart

server response for single object
{
  "data": {}
  "message": null,
  "success": true,
}


@JsonSerializable(genericArgumentFactories: true)
class SingleResponse<T> extends BaseResponse {
  T data;

  SingleResponse({
    String message,
    bool success,
    this.data,
  }) : super(message: message, success: success);

  factory SingleResponse.fromJson(Map<String, dynamic> json, Function(Map<String, dynamic>) create) {
    return SingleResponse<T>(
        success: json["success"],
        message: json["message"],
        data: create(json["data"]));
  }
}

data_response.dart

class DataResponse<T> {
  Status status;
  T res; //dynamic
  String loadingMessage;
  GeneralError error;

  DataResponse.init() : status = Status.Init;

  DataResponse.loading({this.loadingMessage}) : status = Status.Loading;

  DataResponse.success(this.res) : status = Status.Success;

  DataResponse.error(this.error) : status = Status.Error;


  @override
  String toString() {
    return "Status : $status \n Message : $loadingMessage \n Data : $res";
  }
}

enum Status {
  Init,
  Loading,
  Success,
  Error,
}

or if using freeezed then data_response can be

@freezed
abstract class DataResponse<T> with _$DataResponse<T> {
  const factory DataResponse.init() = Init;
  const factory DataResponse.loading(loadingMessage) = Loading;
  const factory DataResponse.success(T res) = Success<T>;
  const factory DataResponse.error(GeneralError error) = Error;
}

Usage: (part of retrofit library

@GET(yourEndPoint)
Future<SingleResponse<User>> getUser();

@GET(yourEndPoint)
Future<ListResponse<User>> getUserList();

If don't use reftofit:

const _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
final _data = <String, dynamic>{};
final _result = await _dio.request<Map<String, dynamic>>('$commentID',
    queryParameters: queryParameters,
    options: RequestOptions(
        method: 'GET',
        headers: <String, dynamic>{},
        extra: _extra,
        baseUrl: baseUrl),
    data: _data);
final value = SingleResponse<Comment>.fromJson(
  _result.data,
  (json) => Comment.fromJson(json),
);
like image 35
Erfan Eghterafi Avatar answered Aug 27 '26 22:08

Erfan Eghterafi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!