Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to parsed nested json to a class with generics?

I'm wondering how can I parse a nested json to a class with generic types. My intention is to wrap responses from the backend (like loginRespose that contains a token) with a code and a message

I have

class BaseResponse<T>{
  int code;
  String message;
  T responseObject;

  BaseResponse.fromJson(Map<String, dynamic> parsedJson)
    : code = parsedJson['Code'],
      message = parsedJson['Message'],
      responseObject = T.fromJson(parsedJson['ResponseObject']); //This is what I'd like to do
}

Obviously the last line throws an error because T doesn't has a named constructor "fromJson". I tried adding some restrictions to the Type but I didn't find any solutions. Do you have any idea on how to pull this off?

like image 634
Sebastian Avatar asked Jul 28 '18 21:07

Sebastian


People also ask

What does fromJson do in flutter?

fromJson() constructor, for constructing a new User instance from a map structure. A toJson() method, which converts a User instance into a map.


2 Answers

You can't do such thing, at least not in flutter. As dart:mirror is disabled and there's no interface for classes constructors.

You'll have to take a different route.

I'll recommend using POO instead. You would here give up on deserializing responseObject from your BaseResponse. And then have subclass of BaseResponse handles this deserialization

Typically you'd have one subclass per type:

class IntResponse extends BaseResponse<int> {
  IntResponse.fromJson(Map<String, dynamic> json) : super._fromJson(json) {
    this.responseObject = int.parse(json["Hello"]);
  }
}

You can then hide this mess away by adding a custom factory constructor on BaseResponse to make it more convenient to use.

class BaseResponse<T> {
  int code;
  String message;
  T responseObject;

  BaseResponse._fromJson(Map<String, dynamic> parsedJson)
      : code = parsedJson['Code'],
        message = parsedJson['Message'];

  factory BaseResponse.fromJson(Map<String, dynamic> json) {
    if (T == int) {
      return IntResponse.fromJson(json) as BaseResponse<T>;
    }
    throw UnimplementedError();
  }
}

Then either instantiate the wanted type directly, or use the factory constructor :

final BaseResponse foo = BaseResponse.fromJson<int>({"Hello": "42", "Code": 42, "Message": "World"});
like image 57
Rémi Rousselet Avatar answered Oct 15 '22 02:10

Rémi Rousselet


You can achieve this with the built_value package (you'll also need built_value_generator and build_runner). Your class will look something like this:

part 'base_response.g.dart';

abstract class BaseResponse<T> implements Built<BaseResponse<T>, BaseResponseBuilder<T>> {
  int code;
  String message;
  T responseObject;

  factory BaseResponse([updates(BaseResponseBuilder<T> b)]) = _$BaseResponse<T>;

  static Serializer<BaseResponse> get serializer => _$baseResponseSerializer;
}

You will have to run flutter packages pub run build_runner build to make the generated file. Then you use it like this:

BaseResponse baseResponse = serializers.deserialize(
  json.decode(response.body),
  specifiedType: const FullType(BaseResponse, const [FullType(ConcreteTypeGoesHere)])
);

There's just one more bit of boilerplate you have to take care of. You need another file called serializers.dart. You need to manually add all the classes you want to deserialize here, and also an addBuilderFactory function for each class that takes a type parameter - and for each concrete type you want to use.

part 'serializers.g.dart';

@SerializersFor(const [
  BaseResponse,
  ConcreteTypeGoesHere,
])
final Serializers serializers = (_$serializers.toBuilder()
      ..addBuilderFactory(
        FullType(BaseResponse, const [const FullType(ConcreteTypeGoesHere)]),
        () => new BaseResponseBuilder<ConcreteTypeGoesHere>()
      )
      ..addPlugin(StandardJsonPlugin()))
    .build();

Then re-run flutter packages pub run build_runner build

Makes me wish for Gson... :S

like image 41
Carson Holzheimer Avatar answered Oct 15 '22 03:10

Carson Holzheimer