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

后端 未结 2 995
自闭症患者
自闭症患者 2020-12-17 14:10

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

相关标签:
2条回答
  • 2020-12-17 14:57

    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"});
    
    0 讨论(0)
  • 2020-12-17 15:01

    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

    0 讨论(0)
提交回复
热议问题