'The argument type 'Serializer<dynamic>' can't be assigned to the parameter type 'Serializer<SingleItemType>'

I am using built_value for serialisation, Initially the code had been working fine, however since I upgraded to null safety I have been getting this error The argument type 'Serializer' can't be assigned to the parameter type 'Serializer'

Here is the code :

import 'package:chopper/chopper.dart';
import 'package:built_collection/built_collection.dart';
import 'package:flutter_netflix_responsive_ui/models/serializers.dart';

class BuiltValueConverter extends JsonConverter {

@override
Response<BodyType> convertResponse<BodyType, SingleItemType>(
  Response response) {


final Response dynamicResponse = super.convertResponse(response);
final BodyType customBody =
    _convertToCustomObject<SingleItemType>(dynamicResponse.body);

return dynamicResponse.copyWith<BodyType>(body: customBody);
}

dynamic _convertToCustomObject<SingleItemType>(dynamic element) {
// If the type which the response should hold is explicitly set to a dynamic Map,
// there's nothing we can convert.
if (element is SingleItemType) return element;

if (element is List)
  return _deserializeListOf<SingleItemType>(element);
else
  return _deserialize<SingleItemType>(element);
}

  BuiltList<SingleItemType> _deserializeListOf<SingleItemType>(
  List dynamicList,
  ) {
// Make a BuiltList holding individual custom objects
  return BuiltList<SingleItemType>(
  dynamicList.map((element) => _deserialize<SingleItemType>(element)),
);
  }

SingleItemType? _deserialize<SingleItemType>(
Map<String, dynamic> value,
) {
// We have a type parameter for the BuiltValue type
// which should be returned after deserialization.
return serializers.deserializeWith<SingleItemType>(
  serializers.serializerForType(SingleItemType)!,
  value,
);
}
}


Solution 1:[1]

I was actually able to solve this just a few minutes ago, it seems like null safety is messing with this. Just simply rewrite serializers.serializerForType(SingleItemType)! to serializers.serializerForType(InnerType) as Serializer<InnerType>. I am not sure why this makes a difference, but its working for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Krypton