参数类型"序列化程序"不能<dynamic>分配给参数类型'Serializer<SingleItemType>'



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

实际上,就在几分钟前,我还能够解决这个问题,看起来零安全性正在扰乱这个问题。只需简单地将serializers.serializerForType(SingleItemType)!重写为serializers.serializerForType(InnerType) as Serializer<InnerType>即可。我不确定为什么这会有所不同,但它对我有效。

相关内容

最新更新