' 当在颤动中重置密码时,flutter"/>

首页 > 解决方案 > 未处理的异常:“字符串”类型不是“地图”类型的子类型' 当在颤动中重置密码时

问题描述

我尝试使用颤振从 API 重置密码,但我得到了结果

Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 1 of 783 libraries in 1,240ms.
I/flutter ( 8568): {success: 1, message: Successfully Reset Password!}
E/flutter ( 8568): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
E/flutter ( 8568): #0      UserResult.sqlreset (package:fluttermysql/models/sql_model.dart:66:42)
E/flutter ( 8568): <asynchronous suspension>
E/flutter ( 8568): #1      _ResetPasswordState.reset (package:fluttermysql/view/ResetPassword.dart:56:5)
E/flutter ( 8568): <asynchronous suspension>
E/flutter ( 8568): 

这是我的模型

class UserResult {
  bool status;
  int success;
  String message;
  String emailAPI;
  String password;
  String phone_number;
  UserResult(
      {this.status, this.emailAPI, this.password, this.message, this.phone_number, this.success});

  factory UserResult.createPostResult(Map<String, dynamic> data) {
    return UserResult(
      status : data['status'],
      success : data['success'],
      message : data['message'],
      emailAPI: data["email"],
      password: data["password"],
      phone_number: data["phone_number"],
    );
  }

这是我的重置密码功能

static Future<UserResult> sqlreset(
      {String email, String password, String idUser, String url}) async {
    var response;
    if (email != null && password != null) {
      response = await http.post(url, body: jsonEncode(<String, String>{
        'email': email,
        'password': password,
      }));
      if (response.statusCode == 200) {
        print(json.decode(response.body));
      } else {
        print(response.statusCode);
      };
      return UserResult.createPostResult(jsonDecode(json.encode(response.body.toString())));

    }
    response = await http.post(url, body: {
      'email': "${email}",
      'password': "${password}",
    });
    return UserResult.createPostResult(jsonDecode(json.encode(response.body.toString())));
  }

json输出有问题吗?我还是不明白控制台日志中的错误是什么谢谢

标签: flutter

解决方案


仅使用

return UserResult.createPostResult(jsonDecode(response.body));

因为你不需要一次又一次地编码和解码


推荐阅读