Flutter + Chopper POST API not working in web

Viewed 4716

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");
1 Answers

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)), 
}

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');
}

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.

Related