我试图使用build_value序列化json响应。 我创建了此类: 1.- SigIn类。 2.- SigninDataSource。 3.-序列化器。
服务运行良好,我得到了答复:
{"status": 200,
"user_id": "****",
"id_token": "****",
"refresh_token": "****",
"expires_in": 3600 }
问题是当我尝试序列化response.body时。
当我尝试这样做时,出现此错误:
[ERROR:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:错误状态:'SignIn'没有序列化器
这是我正在使用的代码: 1.-登录
abstract class SignIn implements Built<SignIn, SignInBuilder>{
static Serializer<SignIn> get serializer => _$signInSerializer;
@nullable
int get status;
@BuiltValueField(wireName: 'user_id')
String get userId;
@BuiltValueField(wireName: 'id_token')
String get idToken;
@BuiltValueField(wireName: 'refresh_token')
String get refreshToken;
SignIn._();
factory SignIn([updates(SignInBuilder b)]) = _$SignIn;
static SignIn fromJson(String jsonString) {
return serializers.deserializeWith(SignIn.serializer,
json.decode(jsonString));
}
}
2。-序列化器 部分'serializers.g.dart';
@SerializersFor(const [SignIn,])
final Serializers serializers =
(_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
3.- SigninDataSource
class SigninDataSource {
Future<SignIn> signIn ({
String email = '***@hotmail.com',
String apiKey = '****',
String password = '****',
}) async {
var url = "https://api-dev.com/signin";
var body = json.decode('{"email": "$email", "password":
"$password"}');
http.post(url, body: json.encode(body),
headers: {
"x-api-key": apiKey,
"content-type" : "application/json",})
.then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
return SignIn.fromJson(response.body);
}
);
}
}
我希望有人可以解决这个问题。