为什么我在尝试调用带有颤振斩波器和构建值的方法时会出错



我得到下面的错误,我认为这是因为空安全性,这意味着没有收到任何数据,或者getSignedInUser((方法不正确,或者classBuiltValueConverter是原因。

(我用Postman测试了代币并检索了数据(

E/flutter(21792(:[错误:flutter/lib/ui/ui_start_state.cc(186(]未处理的异常:未处理的错误在"AuthBloc"的实例中出现null值。E/flutter(21792(:#0
BuiltValueConverter_反序列化软件包:todo_list_app/…/auth/built_value_converter.dart:51 E/flutter(21792(:#1 BuiltValueConverter_convertToCustomObject软件包:todo_list_app/…/auth/built_value_converter.dart:36E/flutter(21792(:#2 BuiltValueConverter.convertResponse包:todo_list_app/…/auth/built_value_converter.dart:25

我正在使用内置值Pubs的斩波器。我正在尝试将即将到来的json转换为object。示例:

{"用户名":"Jack";,"电子邮件":"jack2066@gmail.com&";,"userRole":"用户";,"created_at":"2021-07-03T16:49:56.774Z";,"updated_at":"2021-07-03T16:49:56.774Z"}

下面的代码中错误的开始也在这个教程转换器&构建价值集成

import 'package:built_collection/built_collection.dart';
import 'package:chopper/chopper.dart';
import 'package:todo_list_app/infrastructure/remote/auth/models/serializers.dart';
class BuiltValueConverter extends JsonConverter {
@override
Request convertRequest(Request request) {
return super.convertRequest(
request.copyWith(
body: serializers.serializeWith(
serializers.serializerForType(request.body.runtimeType)!,
request.body,
),
),
);
}
@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 (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(
serializers.serializerForType(SingleItemType)!, // Error Start
value,
);
}
}

这是我的Chopper服务代码,getSignedInUser抛出错误。我不知道getSignedInUser方法的实现是否正确。

import 'package:chopper/chopper.dart';
import 'package:injectable/injectable.dart';
import 'package:todo_list_app/infrastructure/remote/auth/built_value_converter.dart';
import 'package:todo_list_app/infrastructure/remote/auth/models/auth_built_model.dart';
import 'package:todo_list_app/infrastructure/remote/core/constants.dart';
part 'auth_service.chopper.dart';
@singleton
@ChopperApi() 
abstract class AuthService extends ChopperService {
@factoryMethod
static AuthService create() {
final client = ChopperClient(
baseUrl: AUTH_BASE_URL_External,
services: [
_$AuthService(),
],
converter: BuiltValueConverter(),
errorConverter: JsonConverter(),
interceptors: [
HttpLoggingInterceptor()
],
);
return _$AuthService(client);
}
@Post(path: '/signup')
Future<Response> signUp(@Body() RegisterRequestModel body);
@Post(path: '/signin')
Future<Response<LoginResponseModel>> singIn(@Body() LoginRequestModel body);
//headers: {AUTH_HEADER:BEARER+'authValue'}
@Get(path: '/auth', )//authValue='Bearer Token'
Future<Response<UserResponseModel>> getSignedInUser(@Header("Authorization") String authValue);//error Unhandled error Null check operator used on a null value
}

知道如何解决这个错误吗?还有Chopper或其他酒吧是否有良好的文档?感谢

我认为您的"serializers.dart";文件应该如下:

@SerializersFor(const[RegisterRequestModel,LoginRequestModel,UserResponseModel])
final Serializers serializers =
(_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

是的,正如你所说,这是因为零安全。_deserialize返回非null值,而serializers.deserializeWith可以返回null值。您可以将返回类型更改为可为null,也可以处理serializers.deserializeWith的null大小写。如果您愿意,可以使用下面的BuiltValueConverter。这是从他们给出的例子中得出的,它是通用的,有一些小的变化。

import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:chopper/chopper.dart';
import 'package:graphical_representation/app/serializers.dart';
class BuiltValueConverter extends JsonConverter {
T? _deserialize<T>(dynamic value) => serializers.deserializeWith<T>(
(serializers.serializerForType(T) as Serializer<T>),
value,
);
BuiltList<T> _deserializeListOf<T>(Iterable value) => BuiltList(
value.map((value) => _deserialize<T>(value)).toList(growable: false),
);
dynamic _decode<T>(entity) {
/// handle case when we want to access to Map<String, dynamic> directly
/// getResource or getMapResource
/// Avoid dynamic or unconverted value, this could lead to several issues
if (entity is T) return entity;
try {
if (entity is List) return _deserializeListOf<T>(entity);
return _deserialize<T>(entity);
} catch (e) {
print(e);
return null;
}
}
@override
Response<ResultType> convertResponse<ResultType, Item>(Response response) {
// use [JsonConverter] to decode json
final jsonRes = super.convertResponse(response);
final body = _decode<Item>(jsonRes.body);
return jsonRes.copyWith<ResultType>(body: body);
}
@override
Request convertRequest(Request request) => super.convertRequest(
request.copyWith(
body: serializers.serialize(request.body),
),
);
}

相关内容

  • 没有找到相关文章

最新更新