我正在使用built_value 进行序列化,最初代码运行良好,但是自从我升级到空安全性后,我一直收到此错误参数类型'Serializer' 不能分配给参数类型'Serializer'
这是代码:
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,
);
}
}