Search code examples
flutterdartdio

API is calling and getting response but no data is saved in the database


I'm tying to call an PATCH API in my flutter project with help of Dio library. It is an API to update customer details. It is getting 200 status code and getting proper response, but data is not saving in the database. API is working fine when called from POSTMAN. It is just not working in my flutter project. I have tried clearing cache, uninstall and reinstall and tried different login as well. What could be the reason for this weird behavior.

This is API functions

Future updateCustomer(Map<String, dynamic> body) async {
    try {
      final Response res =
          await _dioClient.patch(Endpoints.updateCustomer, data: body);
      return true;
    } catch (e) {
      return false;
    }
  }

This PATCH function

 Future<Response> patch(
    String uri, {
    data,
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    final String key = sprintf('%s+%s', ['get', uri]);
    try {
      Future _future;
      if (running.containsKey(key)) {
        _future = running[key]!;
      } else {
        _future = _dio.patch(
          uri,
          data: data,
          queryParameters: queryParameters,
          options: options,
          cancelToken: cancelToken,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress,
        );
        running[key] = _future;
      }
      Response res = await _future;
      running.remove(key);
      return res;
    } catch (e) {
    
      running.remove(key);
      throw e;
    }
  }

Calling the API

if (_formKey.currentState!.validate()) {
                          Map<String, dynamic> body = {
                            "source_of_fund": sofValue,
                            "is_the_user_owner": behalfValue,
                            "political_association": pepValue,
                          };
                          var res = await context
                              .read<MainStore>()
                              .updateCustomer(body: body);

                          if (res) {
                            context.pushReplacementNamed('Nominee');
                          } else {
                            print("$res");
                          }
                        }

Solution

  • You need to add Cache-control header key.

    Like this _dio.options.headers['Cache-Control'] = "no-cache";

    Hope this helps you.