当前位置: 首页 > 知识库问答 >
问题:

_InternalLinkedHashMap”在连接到API时不是“String”类型的子类型

咸亦
2023-03-14

我正在尝试使用节点登录我的RESTAPI。js。但它给了我一个错误的说法:

[错误:flatter/lib/ui/ui\u dart\u state.cc(186)]未处理的异常:类型'\u InternalLinkedHashMap

我使用提供者作为我的状态管理解决方案。注册用户进展顺利,但登录功能有问题。

class AuthProvider extends ChangeNotifier {
  Status _status = Status.Uninitialized;
  String _token;
  NotificationText _notification;

  Dio dio = Dio();

  Status get status => _status;
  String get token => _token;

  final String api = "https://gentle-thicket-78744.herokuapp.com/api/auth/";

  initAuthProvider() async {
    getToken().then((value) {
      String token = value;
      if (token != null) {
        _token = token;
        _status = Status.Authenticated;
      } else {
        _status = Status.Unauthenticated;
      }
      notifyListeners();
    });
  }

  //Login user
  Future<bool> login(var email, var password) async {
    _status = Status.Authenticating;
    _notification = null;
    notifyListeners();

    final url = "$api/login";

    var body = {
      'email': email,
      'password': password,
    };

    final response = await dio.post(url, data: body);
    print(response.statusCode);
    if (response.statusCode == 200) {
      print("Hello");
      var apiResponse = json.decode(response.data);
      print(apiResponse);
      print("SEcond");

       _status = Status.Authenticated;

      _token = apiResponse['token']; 
 
      await storeUserData(apiResponse);
      notifyListeners();
      return true;
    }
    if (response.statusCode == 401 || response.statusCode == 400) {
      _status = Status.Unauthenticated;
      // Alert dialog
      _notification = NotificationText('Invalid email or password.');
      notifyListeners();
      return false;
    }
    _status = Status.Unauthenticated;
    _notification = NotificationText('Server error.');
    notifyListeners();
    return false; 
    }

  Future<Map> register(String name, int budget, String currency, String email,
      String password, String confirmPassword) async {
    final url = "$api/register";

    Map<String, dynamic> body = {
      'name': name,
      'budget': budget,
      'currency': currency,
      'email': email,
      'password': password,
      'confirmPassword': confirmPassword,
    };

    Map<String, dynamic> result = {
      "success": false,
      "message": 'Unknown error.'
    };

    final response = await dio.post(
      url,
      data: body,
    );

    if (response.statusCode == 201) {
      notifyListeners();
      result['success'] = true;
      return result;
    }
    Map apiResponse = json.decode(response.data);

    if (response.statusCode == 422) {
      if (apiResponse['errors'].containsKey('email')) {
        result['message'] = apiResponse['errors']['email'][0];
        return result;
      }

      if (apiResponse['errors'].containsKey('password')) {
        result['message'] = apiResponse['errors']['password'][0];
        return result;
      }

      return result;
    }

    return result;
  }



  storeUserData(apiResponse) async {
    SharedPreferences storage = await SharedPreferences.getInstance();
    await storage.setString('token', apiResponse['token']);
    await storage.setString('name', apiResponse['name']);
  }

  Future<String> getToken() async {
    SharedPreferences storage = await SharedPreferences.getInstance();
    String token = storage.getString('token');
    return token;
  }

我已经尝试了多种方法来解决这个问题,但运气不好,请帮忙。

共有1个答案

娄森
2023-03-14

dio返回响应。数据已作为地图,因此:

Map apiResponse = response.data; // instead of: var apiResponse = json.decode(response.data);
 类似资料: