createHttpClient static method

dynamic createHttpClient()

Creates an HTTP client that works with Joker stubs.

For web platform only - On native platforms (mobile, desktop, server), you can use the standard http.Client() directly as Joker uses HttpOverrides to intercept all requests automatically.

On web, use this factory method to create clients that will respect Joker stubs:

// In your service/repository layer with dependency injection
class ApiService {
  final http.Client client;

  ApiService(this.client);

  Future<User> getUser(int id) async {
    final response = await client.get(
      Uri.parse('https://api.example.com/users/$id'),
    );
    return User.fromJson(jsonDecode(response.body));
  }
}

// In main.dart or dependency injection setup
void main() {
  if (kDebugMode) {
    Joker.start();
    // Add your stubs here
  }

  // Inject the client
  final httpClient = Joker.createHttpClient();
  final apiService = ApiService(httpClient);

  runApp(MyApp(apiService: apiService));
}

This method requires the joker_http package to be installed. Add it to your pubspec.yaml:

dependencies:
  joker_http: ^0.0.1

Implementation

static dynamic createHttpClient() {
  throw UnimplementedError(
    'createHttpClient() requires the joker_http package.\n'
    'Add it to your pubspec.yaml:\n'
    'dependencies:\n'
    '  joker_http: ^0.0.1\n\n'
    'Then use: import \'package:joker_http/joker_http.dart\' as joker_http;\n'
    'And call: joker_http.createHttpClient()',
  );
}