joinUrl method

String joinUrl(
  1. String baseUrl,
  2. String path
)

Joins a baseUrl and path into a single URL string.

Handles trailing/leading slashes appropriately to avoid duplicates.

  • baseUrl: The base URL (trailing slash will be stripped if present)
  • path: The endpoint path (leading slash will be added if missing)
  • Returns: Properly joined URL string

Implementation

String joinUrl(String baseUrl, String path) {
  baseUrl = baseUrl.endsWith('/')
      ? baseUrl.substring(0, baseUrl.length - 1)
      : baseUrl;
  path = path.startsWith('/') ? path : '/$path';
  return '$baseUrl$path';
}