getService<ServiceType extends ChopperService> method

ServiceType getService<ServiceType extends ChopperService>()

Retrieve any service included in the ChopperClient

final chopper = ChopperClient(
  baseUrl: 'localhost:8000',
  services: [
    // Add a generated service
    TodosListService.create()
  ],
);

final todoService = chopper.getService<TodosListService>();

Implementation

ServiceType getService<ServiceType extends ChopperService>() {
  final serviceType = _typeOf<ServiceType>();
  if (serviceType == dynamic || serviceType == ChopperService) {
    throw Exception(
        'Service type should be provided, `dynamic` is not allowed.');
  }
  final service = _services[serviceType];
  if (service == null) {
    throw Exception('Service of type \'$serviceType\' not found.');
  }
  return service as ServiceType;
}