JsonRequest constructor

JsonRequest({
  1. TransportPlatform? transportPlatform,
})

Implementation

factory JsonRequest({TransportPlatform? transportPlatform}) {
  // If a transport platform is not explicitly given, fallback to the globally
  // configured platform.
  transportPlatform ??= globalTransportPlatform;

  if (MockTransportsInternal.isInstalled) {
    // If transports are mocked, return a mock-aware JsonRequest instance.
    // This mock-aware instance will be able to decide at the time of dispatch
    // whether or not the mock logic should handle the request.
    return MockAwareTransportPlatform.newJsonRequest(transportPlatform);
  } else if (transportPlatform != null) {
    // Otherwise, return a real instance using the given transport platform.
    return transportPlatform.newJsonRequest();
  } else {
    // If transports are not mocked and a transport platform is not available
    // (neither explicitly given nor configured globally), then we cannot
    // successfully construct a JsonRequest.
    throw TransportPlatformMissing.httpRequestFailed('JsonRequest');
  }
}