Flutter + Chopper POST API在网络中不起作用 [英] Flutter + Chopper POST API not working in web
问题描述
我对扑扑完全陌生.在学习Flutter时,我无法使用Flutter Web应用程序登录,同样的代码在android移动应用程序中也能正常工作.
I am completely new to flutter. While learning flutter I am not able to login with flutter web application same code is working fine with the android mobile app.
请找到以下日志信息.相同的调用在除flutter web之外的所有平台上均有效
Please find the below log info. The same call is working on all platforms except flutter web
INFO:2020-05-22 17:40:48.229:curl -v -X POST -H'content-type:application/json;charset = UTF-8'-H'接受:/'-H'缓存控制:无缓存'-H'连接:keep-alive'-d'{用户名":"admin,"密码:" admin @ 123}' http://healthvedic.in/api/admin/user/login.php
INFO: 2020-05-22 17:40:48.229: curl -v -X POST -H 'content-type: application/json; charset=UTF-8' -H 'Accept: /' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -d '{"username":"admin","password":"admin@123"}' http://healthvedic.in/api/admin/user/login.php
类型为'String'的期望值,但类型为'ClientException'
The expected value of type 'String', but got one of type 'ClientException'
import 'package:chopper/chopper.dart';
part 'chopper_network_manager.chopper.dart';
@ChopperApi(baseUrl: '')
abstract class ChopperNetworkManager extends ChopperService {
static ChopperNetworkManager manager;
@Post(path: 'admin/user/login.php')
Future<Response> doLogin(@Body() Map<String, dynamic> body);
static var customHeaders = {
'content-type': 'application/json; charset=UTF-8',
'Accept': '*/*',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
};
static ChopperNetworkManager create() {
final client = ChopperClient(
baseUrl: 'http://healthvedic.in/api/',
services: [
_$ChopperNetworkManager(),
],
converter: JsonConverter(),
interceptors: [
HeadersInterceptor(customHeaders),
CurlInterceptor(),
]);
return _$ChopperNetworkManager(client);
}
static ChopperNetworkManager getInstance() {
if (manager == null) {
manager = ChopperNetworkManager.create();
return manager;
}
return manager;
}
}
呼叫地点
Calling Place
void doLogin() async {
LoginReqModel reqModel = LoginReqModel(
username: userNameController.text, password: passwordController.text);
var res = ChopperNetworkManager.getInstance().doLogin(reqModel.toJson());
res.then(
(value) => {
updateOnUI(LoginResModel.fromJson(value.body)),
}, onError: (e) {
onError(e);
}).catchError(onError, test: (error) => onError(error));
res.catchError(onError(''));
}
下面是PHP Rest-API中的标头
Below are the headers in PHP Rest-API
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, Access-Control-Allow-Origin");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST, OPTIONS");
推荐答案
我无法运行您提供的代码,但是给出的错误非常清楚:类型为'String'的期望值,但类型为'ClientException'
之一.如错误所提到的,它期望一个'String',但是抛出一个异常.
I'm unable to run the code that you've provided, but the error given is pretty clear: The expected value of type 'String', but got one of type 'ClientException'
. As mentioned in the error, it expects a 'String' but an Exception was thrown instead.
在评论中,您提到导致问题的行是
On the comments, you've mentioned that the line causing the issue was
res.then((value) => {
updateOnUI(LoginResModel.fromJson(value.body)),
}
由于该应用程序可以在Android上正常运行,但不能在Web上运行,因此建议您检查在LoginResModel中发出的请求,并查看为什么抛出ClientException.添加 try-catch
块也可能有帮助.
Since the app works fine in Android but not on Web, I suggest checking the request being made in LoginResModel and see why a ClientException was thrown. Adding a try-catch
block may also help.
try {
// line causing the issue
} catch (error){
debugPrint('Error: $error');
}
我很好奇为什么从 ChopperNetworkManager.getInstance().doLogin(reqModel.toJson())
抛出ClientException,也许您可以在 doLogin()<
并找出其原因.
I'm curious on why ClientException is being thrown from ChopperNetworkManager.getInstance().doLogin(reqModel.toJson())
, perhaps you can catch the Exception inside doLogin()
and identify its cause.
这篇关于Flutter + Chopper POST API在网络中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!