Я работаю с api в качестве бэкэнда для получения пользовательских данных при входе в систему.
Это код для получения данных:
Future<dynamic> getLoginData(String _phone, bool isPhone) async {
String endPoint = isPhone
? '/UserLogin_Mobile?PhoneNo=$_phone'
: '/UserLogin?Email=$_phone';
var response = await client
.get('$ENDPOINT' + endPoint, headers: headers)
.catchError((onError) {
print(onError.toString());
});
print("response=>" + response.body);
if (json.decode(response.body).toString().contains("registered"))
print("\n\nReg");
if (response.statusCode == 200) {
if (json.decode(response.body).toString().contains("not found"))
print("\n\nNot found");
return User.fromJson(jsonDecode(response.); //TODO getting error here
}
return null;
}
Это модель пользователя
class User {
double id;
String userPhone;
String userPass;
String userNicename;
String userEmail;
String userUrl;
String userRegistered;
String userActivationKey;
int userStatus;
String displayName;
String errorDis;
bool isSuccessfull = true;
String statuscode;
String message;
String latitude;
String longitude;
User(
{this.id,
this.userPhone,
this.userPass,
this.displayName,
this.userActivationKey,
this.userEmail,
this.userNicename,
this.userRegistered,
this.userStatus,
this.statuscode,
this.message,
this.userUrl});
User.initial()
: id = 0,
userPhone = '',
userPass = '',
userNicename = '',
userEmail = '',
userUrl = '',
statuscode = '',
userRegistered = '',
userActivationKey = '',
userStatus = 0,
displayName = '';
User.fromJson(Map<String, dynamic> json)
: id = json['ID'],
userPhone = json['user_login'],
userPass = json['user_pass'],
userNicename = json['user_nicename'],
userEmail = json['user_email'],
userUrl = json['user_url'],
userRegistered = json['user_registered'],
userActivationKey = json['user_activation_key'],
userStatus = json['user_status'],
displayName = json['display_name'],
statuscode = json['statuscode'],
message = json['message'],
latitude = json['latitude'],
longitude = json['longitude'];
User.errorFromServer(Map<String, dynamic> json) {
statuscode = json['statuscode'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['user_login'] = this.userPhone;
data['user_pass'] = this.userPass;
data['user_nicename'] = this.userNicename;
data['user_email'] = this.userEmail;
data['user_url'] = this.userEmail;
data['user_registered'] = this.userRegistered;
data['user_activation_key'] = this.userActivationKey;
data['user_status'] = this.userStatus;
data['display_name'] = this.displayName;
data['latitude'] = this.latitude;
data['longitude'] = this.longitude;
return data;
}
}
И это ошибка отображается в TODO
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
Я получаю эту ошибку, потому что в TODO я пытаюсь сопоставить response.body (который является строкой) с Map ( то есть пользовательские данные).
как решить ??? или мой вопрос неверен?
РЕДАКТИРОВАТЬ это
response.body=> {"ID":633.0,"user_login":"xxxxxxxxxx","user_pass":"xxxxxx","user_nicename":"","user_email":"","user_url":"","user_registered":"0001-01-01T00:00:00","user_activation_key":"","user_status":0,"display_name":"","latitude":null,"longitude":null}