颤振类型'String'不是'FutureOr<Model>'类型的子类型,响应Json不带键



我试着这样调用服务,我觉得这里有问题因为"result"返回未来和不能分配给字符串,或者如何解析未来字符串?MyApi返回没有Key的Json,像这样

身体

ReturnStatus

所以我试图将响应直接存储到用户变量中,但仍然不能工作

UI (Button Login)

login.loginProcess
? Center(child: CircularProgressIndicator())
: RaisedButton(
color: myPrimaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Login",
style: TextStyle(
color: Colors.black,
fontFamily: "NunitoSansBold")),
],
),
padding:
EdgeInsets.only(top: 16.0, bottom: 16.0),
onPressed: () {
print("clicked Button Login");
**login.authenticationUser(context);**
},
),

服务
class AuthService {
Future<User> authenticateUser(String id, String password) async {
var user;
try {
final resAuth = await http.post(
"${BaseUrl.auth}api/AuthenticateUser",
body: {"login": id, "password": password},
);
if (resAuth.statusCode == 200) {
user = resAuth.body;
}
} catch (e) {
return user;
}
return user;
}
}

带有ChangeNotifier的提供者(用于处理业务逻辑和东西)

authenticationUser(BuildContext context) async {
if (_controllerEmail.text != "" && _controllerPassword.text != "") {
loginProcess = true;
final ioc = new HttpClient();
ioc.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
//TODO StoredSharedPreference
SharedPreferences _preferences = await SharedPreferences.getInstance();
try {
AuthService authService = new AuthService();
var result = authService.authenticateUser(
_controllerEmail.text, _controllerPassword.text);
showSnackBar(result);
_preferences.setString("status", "seen");
} catch (e) {
print(e);
showSnackBar(result.toString());
}
loginProcess = false;
} else {
autoValidate = true;
}
} 

模型

class User {
String status;
String id;
String userName;
User({
this.status,
this.id,
this.userName,
});
factory User.fromJson(Map<String, dynamic> json) =>
User(status: json["status"], id: json["id"], userName: json["userName"]);
Map<String, dynamic> toJson() =>
{"status": status, "id": id, "userName": userName};
}

=======更新,修改方法authenticationUser (add await)=======

服务
class AuthService {
Future<User> authenticateUser(String id, String password) async {
var user;
try {
final resAuth = await http.post(
"${BaseUrl.auth}api/AuthenticateUser",
body: {"login": id, "password": password},
);
if (resAuth.statusCode == 200) {
user = resAuth.body;
}
} catch (e) {
return user;
// throw Exception(e.toString());
}
return user;
}
}
提供者

authenticationUser(BuildContext context) async {
if (_controllerEmail.text != "" && _controllerPassword.text != "") {
loginProcess = true;
final ioc = new HttpClient();
ioc.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
//TODO StoredSharedPreference
SharedPreferences _preferences = await SharedPreferences.getInstance();
try {
AuthService authService = new AuthService();
var result = await authService.authenticateUser(
_controllerEmail.text, _controllerPassword.text);
_preferences.setString("status", "seen");
showSnackBar(result.toString());
} catch (e) {
print(e);
/* showSnackBar(e.toString());*/
}
loginProcess = false;
} else {
autoValidate = true;
}
}

在catch (e) {打印(e);

e的值为String' is not a subtype of type 'FutureOr<User>'

==Update Add Image Value from resAuth==

resAuthValue

您可能需要添加await

var result = await authService.authenticateUser(
_controllerEmail.text, _controllerPassword.text);

编辑

authenticateUser方法更改为

Future authenticateUser(String id, String password) async {
var user;
try {
final resAuth = await http.post(
"${BaseUrl.auth}api/AuthenticateUser",
body: {"login": id, "password": password},
);
if (resAuth.statusCode == 200) {
return resAuth.body;
}
} catch (e) {
return false;
}
return false;
}

更改提供者代码如下

try {
AuthService authService = new AuthService();
var result = await authService.authenticateUser(
_controllerEmail.text, _controllerPassword.text);
_preferences.setString("status", "seen");
if(result!=false)
showSnackBar(result.toString());
} catch (e) {
print(e);
/* showSnackBar(e.toString());*/
}

最新更新