2

I have this object :

abstract class ApiData<T> implements Built<ApiData<T>, ApiDataBuilder<T>> {
    int get offset;

    int get limit;

    int get total;

    int get count;

    BuiltList<T> get results;

    ApiData._();

    factory ApiData([void Function(ApiDataBuilder<T>) updates]) = _$ApiData<T>;

    static Serializer<ApiData> get serializer => _$apiDataSerializer;
}

This is the code I use to map the dynamic result from the response to an ApiData object :

Future<ApiData<T>> mapDynamic<T>(dynamic value) async {
    return standardSerializers.deserializeWith<ApiData<T>>(
      ApiData.serializer,
      value,
    );
}

But I end up with this error : type '_$ApiDataSerializer' is not a subtype of type 'Serializer<ApiData<MyBuitValueAbstractObject>>'

I tried to add :

static Serializer<ApiData<MyBuitValueAbstractObject>> get apiMyObjectSerializer => _$apiDataMyObjectSerializer;

But that did not work. How can I have my results value deserialized properly

Edit 1 - if I use standardSerializers.deserializeWith<ApiData> (without <T> this time) I get this message error :

Unhandled Exception: Deserializing '[offset, 0, limit, 20, total, 2233, count, 20, results, [{...' to 'ApiData' failed due to: Deserializing '[{...' to 'BuiltList' failed due to: Invalid argument(s): Unknown type on deserialization. Need either specifiedType or discriminator field.

How can I specify a "discriminator field" thing?

Edit 2 - I found a hack to work around it for now, it does not satisfy me, but I can keep working until I find a proper solution : https://github.com/google/built_value.dart/issues/368#issuecomment-486567646

tufekoi
  • 929
  • 2
  • 14
  • 33
  • This might help: https://blog.nikkigoel.com/custom-serializer-with-builtvalue-in-dart-and-flutter Disclaimer: I am the author of this article. – nicks101 May 20 '23 at 10:27
  • Better late than never I guess but I've abandoned Flutter/Dart for years now haha. But thanks for trying to help! – tufekoi May 23 '23 at 04:35

0 Answers0