updateProfileMultiPart function

Future<CommonModel?> updateProfileMultiPart(
  1. {required Map<String, dynamic> value,
  2. File? profileImage,
  3. bool isFromProfile = false,
  4. bool isFromSocialLogin = false,
  5. bool showLoader = true}
)

Implementation

Future<CommonModel?> updateProfileMultiPart({required Map<String, dynamic> value, File? profileImage, bool isFromProfile = false, bool isFromSocialLogin = false, bool showLoader = true}) async {
  CommonModel? res;

  MultipartRequest multiPartRequest = await getMultiPartRequest(APIEndPoint.updateProfile);

  multiPartRequest.fields.addAll(await getMultipartFields(val: value));

  if (profileImage != null) {
    multiPartRequest.files.add(await MultipartFile.fromPath(UserKeys.profileImage, profileImage.path));
  }
  log("${multiPartRequest.fields}");

  if (isFromSocialLogin) {
    Map<String, String> header = {
      HttpHeaders.cacheControlHeader: 'no-cache',
      'Access-Control-Allow-Headers': '*',
      'Access-Control-Allow-Origin': '*',
    };
    header.putIfAbsent(HttpHeaders.authorizationHeader, () => 'Bearer ${appStore.token}');
    header.putIfAbsent(HttpHeaders.contentTypeHeader, () => 'application/json; charset=utf-8');
    header.putIfAbsent(HttpHeaders.acceptHeader, () => 'application/json; charset=utf-8');
    multiPartRequest.headers.addAll(header);
  } else {
    multiPartRequest.headers.addAll(buildHeaderTokens());
  }

  log("Multi Part Request : ${jsonEncode(multiPartRequest.fields)} ${multiPartRequest.files.map((e) => e.field + ": " + e.filename.validate())}");

  if (showLoader) appStore.setLoading(true);

  await sendMultiPartRequest(multiPartRequest, onSuccess: (data) async {
    if (showLoader) appStore.setLoading(false);

    if (data != null) {
      if ((data as String).isJson()) {
        LoginResponse res = LoginResponse.fromJson(jsonDecode(data));

        saveUserData(res.data ?? UserData());
        if (isFromProfile) toast(res.message.validate().capitalizeFirstLetter());
      }
    }

    res = CommonModel.fromJson(jsonDecode(data));
  }, onError: (error) {
    // toast(error.toString(), print: true);
    res = CommonModel.fromJson(jsonDecode(error));
    appStore.setLoading(false);
  }).catchError((e) {
    appStore.setLoading(false);
    toast(e.toString());
  });
  return res;
}