问题
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.
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 'Accept: /' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -d '{"username":"admin","password":"admin@123"}' http://healthvedic.in/api/admin/user/login.php
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(''));
}
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");
来源:https://stackoverflow.com/questions/61954922/flutter-chopper-post-api-not-working-in-web