flutter 如何修复类型'_InternalLinkedHashMap〈String,dynamic>'不是类型'Response '的子类型< dynamic>(单位为肘)?

ylamdve6  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(148)

我有cubits文件和一个从API获取数据异步函数:
来自api的响应数据如下所示:

{"success":true,"userId":782,"accessToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzgyLCJlbWFpbCI6Im5qZGpmam5AZ21haWwuY29tIiwicHJvZHVjdElkIjoxMCwiZXhwIjoxNjY5NDgwMDM5fQ.6anGdvruukEmxj5GZE1ZCGHYdIrEK5lxOPuMNWGfj-g","refreshToken":"dd4f0b69-3607-4b73-9c04-4fc1a9bd52e0"}

当我按下按钮时:

LoginButton(
  onPressed: () async {
   await BlocProvider.of<AuthCubit>(context).signUp();
  }
)

我从api得到200状态代码,我得到错误=〉type '_InternalLinkedHashMap〈String,dynamic〉'不是类型'Response'的子类型
下面是cubits文件中的函数:

Future signUp() async {
    try {
      final Response response =  await _apiService.signUp(
        email: state.email ?? "",
        password: state.password ?? "",
        firstName: state.firstName ?? "",
        lastName: state.lastName ?? "",
        genderUuid: state.genderUuid ?? "",
        ageGroupUuid: state.ageGroupUuid ?? "",
        countryUuid: state.countryUuid ?? ""
      );
      Map<String, dynamic > data = Map<String, dynamic>.from(jsonDecode(response.data));
      if (response.statusCode == 200) {
        log("I am here");
      }
    } on DioError catch (ex) {
      log("cubit === ${ex.response!.data.toString()}");
      emit(ErrorAuthentification(ex.response!.data["statusCode"], ex.response!.data["message"]));
    } catch (e) {
      log(e.toString()); // type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response<dynamic>'
      rethrow; // type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response<dynamic>'
    }
  }

这是我的ApiService:

class ApiService {
  final ApiProvider apiProvider = ApiProvider();

  Future<dynamic> signUp({
    required String email,
    required String password, 
    required String firstName,
    required String lastName,
    required String genderUuid,
    required String ageGroupUuid,
    required String countryUuid
    }) async {
    final data = await apiProvider.signUp(
      email: email,
      password: password,
      firstName: firstName,
      lastName: lastName,
      genderUuid: genderUuid,
      ageGroupUuid: ageGroupUuid, 
      countryUuid: countryUuid
    );
    return data;
  }
}

而我的ApiProvide:

class ApiProvider {
  final dio = Dio(BaseOptions(baseUrl: ApiPath.testUrl, contentType: "application/json"));

  static final _instance = ApiProvider._internal();

  factory ApiProvider() {
    return _instance;
  }

  ApiProvider._internal();

  Future<dynamic> _request(Function request) async {
    try {
      final res = (await request());
      //log('res === ${res.statusCode}, ${res.data.runtimeType}, ${res.data}');
      return res.data;
    } on DioError catch (ex) {
      log('Error dio _request === ${ex.response!.statusCode}');
      rethrow;
    }
  }

  Future<dynamic> signUp({
    required String email,
    required String password,
    required String firstName,
    required String lastName,
    required String genderUuid,
    required String ageGroupUuid,
    required String countryUuid
  }) async {
    var params = {
      "email": email,
      "password": password,
      "firstName": firstName,
      "lastName": lastName,
      "genderUuid": genderUuid,
      "ageGroupUuid": ageGroupUuid,
      "countryUuid": countryUuid,
      "deviceId": "fingerprint"
    };
    dio.interceptors.add(
      LogInterceptor(requestBody: true, responseBody: true),
    );
    try {
      return _request(
        () => dio.post(
          ApiPath.signUp,
          data: jsonEncode(params),
          options: Options(
            responseType: ResponseType.json
          )
        )
      );
    } on DioError catch (ex) {
      log('Error signIn ==== ${ex.response!.statusCode}');
      rethrow;
    }
  }
}

我不明白为什么即使请求成功也会出现此错误(200状态代码)

nlejzf6q

nlejzf6q1#

您已经从_request中的response中获得了数据,因此不需要再次使用它,因此请更改以下内容:

Map<String, dynamic > data = Map<String, dynamic>.from(jsonDecode(response.data));

更改为:

Map<String, dynamic > data = Map<String, dynamic>.from(jsonDecode(response));

相关问题