thenWithExtract<R> method

Future<R?> thenWithExtract<R>(
  1. Future<Response> nextRequest(
    1. M extracted
    ),
  2. R? finalExtractor(
    1. Response response
    )
)

链式调用:传递提取后的对象给下一个请求,并提取最终结果 如果任何一步失败,返回 null

示例:

final imageUrl = await http.isLoading
  .send(...)
  .extractModel<FileUploadResult>(FileUploadResult.fromConfigJson)
  .thenWithExtract<String>(
    (uploadResult) => http.send(
      method: hm.post,
      path: '/get-url',
      data: {'key': uploadResult.imageKey},
    ),
    (response) => response.extractField<String>('image_url'),
  );

注意:如果第一步使用了 http.isLoading.send(),整个链路只显示一个加载提示 在链路结束时(成功或失败)自动关闭

Implementation

Future<R?> thenWithExtract<R>(
  Future<Response<dynamic>> Function(M extracted) nextRequest,
  R? Function(Response<dynamic> response) finalExtractor,
) async {
  // 关键修复:在 await this 之前检查 hasChainLoading,避免 finally 块中的 Future.microtask 清空 _chainLoadingId
  final hasChainLoading = HttpUtilSafeCall.hasChainLoading();

  final extracted = await this;
  if (extracted == null) {
    // 检查是否有链式调用的加载提示,如果有则关闭
    if (hasChainLoading) {
      HttpUtilSafeCall.closeChainLoading();
    }
    return null;
  }

  final nextResponse = await nextRequest(extracted);
  if (!nextResponse.isSuccess) {
    // 失败时关闭加载提示
    if (hasChainLoading) {
      HttpUtilSafeCall.closeChainLoading();
    }
    return null;
  }

  // 成功时关闭加载提示
  if (hasChainLoading) {
    HttpUtilSafeCall.closeChainLoading();
  }

  return finalExtractor(nextResponse);
}