Why I'm getting this error: The argument type 'String' can't be assigned to the parameter type 'int'?
Why I'm getting this error: The argument type 'String' can't be assigned to the parameter type 'int'?
如果用户输入了错误的电子邮件和密码,这是状态代码和正文的响应。 I/flutter (20074): 200 I/flutter (20074): {"code":1,"message":"invalid username or password","data":null}
如果用户输入正确的电子邮件和密码,这就是响应 I/flutter (20074): 200 I/flutter (20074): {"code":0,"message":"success","data":{"Id":121106,"Name":"User 1","Email":"user@gmail.com","Token":"2db0ce86-2dc0-4381-97de-ce6e0c341d90"}}
。如果用户输入了错误的电子邮件和密码,我想验证我的登录。它将显示“无效凭据”并且不会转到另一个页面。我的问题是,当我将此 response.body['code'] == 0
添加到我的 if 语句时,我得到了这个错误 The argument type 'String' can't be assigned to the parameter type 'int'
。我该如何解决这个问题?
创建调用登录的函数postapi
Future<void> login() async {
if(emailController.text.isNotEmpty && passController.text.isNotEmpty) {
var headers = {"Content-type": "application/json"};
var myBody = {
'email' : emailController.text,
'password' : passController.text,
};
var response = await http.post(Uri.parse("url"),
headers: headers,
body: jsonEncode( myBody ));
if(response.statusCode == 200 && response.body['code'] == 0) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()));
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Invalid Credentials.")));
}
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Blank Field Not Allowed")));
}
}
对象response.body
是一个字符串。要访问响应中的单个对象,您首先需要解码 json.
var response = await http.post(Uri.parse("url"),
headers: headers,
body: jsonEncode( myBody ));
final data = json.decode(response.body);
if (response.statusCode == 200 && data['code'] == 0) {
...
}
如果用户输入了错误的电子邮件和密码,这是状态代码和正文的响应。 I/flutter (20074): 200 I/flutter (20074): {"code":1,"message":"invalid username or password","data":null}
如果用户输入正确的电子邮件和密码,这就是响应 I/flutter (20074): 200 I/flutter (20074): {"code":0,"message":"success","data":{"Id":121106,"Name":"User 1","Email":"user@gmail.com","Token":"2db0ce86-2dc0-4381-97de-ce6e0c341d90"}}
。如果用户输入了错误的电子邮件和密码,我想验证我的登录。它将显示“无效凭据”并且不会转到另一个页面。我的问题是,当我将此 response.body['code'] == 0
添加到我的 if 语句时,我得到了这个错误 The argument type 'String' can't be assigned to the parameter type 'int'
。我该如何解决这个问题?
创建调用登录的函数postapi
Future<void> login() async {
if(emailController.text.isNotEmpty && passController.text.isNotEmpty) {
var headers = {"Content-type": "application/json"};
var myBody = {
'email' : emailController.text,
'password' : passController.text,
};
var response = await http.post(Uri.parse("url"),
headers: headers,
body: jsonEncode( myBody ));
if(response.statusCode == 200 && response.body['code'] == 0) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()));
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Invalid Credentials.")));
}
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Blank Field Not Allowed")));
}
}
对象response.body
是一个字符串。要访问响应中的单个对象,您首先需要解码 json.
var response = await http.post(Uri.parse("url"),
headers: headers,
body: jsonEncode( myBody ));
final data = json.decode(response.body);
if (response.statusCode == 200 && data['code'] == 0) {
...
}