flutter 抖动:异常DioError [DioErrorType.DEFAULT]:类型“String”不是类型“Map〈String,dynamic>”的子类型

isr3a4wc  于 2023-02-16  发布在  Flutter
关注(0)|答案(3)|浏览(153)

我是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);
}

这是我的 Package 类

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);

}

这是我的演示者类

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",
  }
}
ig9co6j1

ig9co6j11#

我得到了字符串形式的响应。但我已经使用以下步骤转换为JSON:

final dio = Dio();        
    dio.interceptors.add(JsonResponseConverter());
            
        json_response_convert.dart
        
            import 'package:dio/dio.dart';
            import 'dart:convert';
            
            class JsonResponseConverter extends Interceptor {
              @override
              void onResponse(Response response, ResponseInterceptorHandler handler) {
                response.data = json.decode(response.data);
                super.onResponse(response, handler);
              }
            }
rsl1atfo

rsl1atfo2#

您忘记添加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类。

@JsonSerializable()
class User{
  ...
  User({this.name, this.l_name, this.email, session_token, this.device_token});  
  ...
}
2nc8po8w

2nc8po8w3#

如果您可以将端点响应标头更改为application/json。
问题是端点的头返回text/plain或者只是不返回application/json。
但是为了使改进能够自动解析,它需要这种“application/json”特定类型的头。
希望能有所帮助。

相关问题