_InternalLinkedHashMap<String, dynamic>' 在连接到 API 时不是类型 'String' 的子类型
_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' when connecting to API
我正在尝试使用 node.js 登录我的 RESTAPI。但它给了我一个错误说:
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
我正在使用 Provider 作为我的状态管理解决方案。注册用户进行得很顺利,但登录功能有问题。
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;
}
我尝试了多种方法来解决这个问题,但没有成功,请帮忙。
dio
returns response.data
已经是 Map
,因此:
Map apiResponse = response.data; // instead of: var apiResponse = json.decode(response.data);
我正在尝试使用 node.js 登录我的 RESTAPI。但它给了我一个错误说:
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
我正在使用 Provider 作为我的状态管理解决方案。注册用户进行得很顺利,但登录功能有问题。
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;
}
我尝试了多种方法来解决这个问题,但没有成功,请帮忙。
dio
returns response.data
已经是 Map
,因此:
Map apiResponse = response.data; // instead of: var apiResponse = json.decode(response.data);