gpt4 book ai didi

json - flutter :异常 DioError [DioErrorType.DEFAULT]:类型 'String' 不是类型 'Map' 的子类型

转载 作者:行者123 更新时间:2023-12-04 13:54:53 24 4
gpt4 key购买 nike

我是 flutter 的新手,我无法解决这个问题有人可以帮助我吗?
如果我输入 Future<String> login(),我可以获取字符串中的数据而不是 Future<WrappedResponse> login()这将打印在下面给出的 Presenter 类上。
这是我的 api 类

import 'dart:io';
import 'dart:math';
import 'package:ceee_app/converters/wrapped_response.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:retrofit/retrofit.dart';

part 'api_service.g.dart';

@RestApi(baseUrl: "https://******.com/_dev/api/v1/")
abstract class RestClient {
factory RestClient(Dio dio) = _RestClient;

@FormUrlEncoded()
@POST("login")
Future<WrappedResponse> login(@Field("email") String email, @Field("password") String password, @Field("device_token") String token, @Field("device_type") String type);
}
这是我的包装类
import 'package:ceee_app/model/user.dart';
import 'package:json_annotation/json_annotation.dart';

part 'wrapped_response.g.dart';

@JsonSerializable()

class WrappedResponse{
@JsonKey(name: "message")
String message;
@JsonKey(name: "status")
String status;
@JsonKey(name: "result")
User data;

WrappedResponse();

factory WrappedResponse.fromJson(Map<String, dynamic> json) => _$WrappedResponseFromJson(json);
Map<String, dynamic> toJson() => _$WrappedResponseToJson(this);

}
这是我的用户类
import 'package:json_annotation/json_annotation.dart';

part "user.g.dart";

@JsonSerializable()
class User{
@JsonKey()
int id;
@JsonKey()
String name;
@JsonKey()
String l_name;
@JsonKey()
String email;
@JsonKey()
String session_token;
@JsonKey()
String device_token;

User();

factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

Map<String, dynamic> toJson() => _$UserToJson(this);

}
这是我的 Presenter 类
import 'package:ceee_app/contracts/login_activity_contract.dart';
import 'package:ceee_app/model/user.dart';
import 'package:ceee_app/webservices/api_service.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LoginActivityPresenter implements LoginActivityInteractor {
LoginActivityView view;
LoginActivityPresenter(this.view);
RestClient api = RestClient(Dio());

@override
void success(String token) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("api_token", token);
}

@override
void destroy() => view = null;

@override
void login(String email, String password, String token, String type) async {
await api.login(email, password, token, type).then((it){
debugPrint("Data : "+it.toString());

}).catchError((e){
print("Exception $e");
view?.toast("There is an error!");
});
}
}
我的回答是
{
"status": 1,
"message": "Login successful!",
"result": {
"id": 20,
"session_token": "YXBpX3Rva2VuNWY3YzJmODM2ZTgyNjUuNTY1MjUwNzAxNjAxOTc0MTQ3",
"name": "abd",
"l_name": "xyz",
"email": "abc@gmail.com",
"device_token": "fKrw8mpYT96fWIfaxrF26r:APA91bGZUW1wGSmdNMNb",
}
}

最佳答案

您忘记添加 JSON 序列化所需的构造函数参数。

@JsonSerializable()
class WrappedResponse{
@JsonKey(name: "message")
String message;
@JsonKey(name: "status")
String status;
@JsonKey(name: "result")
User data;

WrappedResponse({this.message, this.status, this.data});

factory WrappedResponse.fromJson(Map<String, dynamic> json) => _$WrappedResponseFromJson(json);
Map<String, dynamic> toJson() => _$WrappedResponseToJson(this);
}
这同样适用于 User类(class)。
@JsonSerializable()
class User{
...
User({this.name, this.l_name, this.email, session_token, this.device_token});
...
}

关于json - flutter :异常 DioError [DioErrorType.DEFAULT]:类型 'String' 不是类型 'Map<String, dynamic>' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64226323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com