getUriFromEndpoints static method

Uri getUriFromEndpoints({
  1. required String endpoint,
  2. Map<String, dynamic>? queryParams,
  3. bool usePrefix = false,
  4. List<String>? pathSeg,
})

it will create uri from given endpoint with including baseurl baseurl can be setup by calling setup function

Implementation

static Uri getUriFromEndpoints({
  required String endpoint,
  Map<String, dynamic>? queryParams,
  bool usePrefix = false,
  List<String>? pathSeg,
}) {
  String? host = _host?.trim();

  // If host contains scheme, parse it
  String scheme = 'https';
  if (host != null && (host.startsWith('http://') || host.startsWith('https://'))) {
    final parsed = Uri.parse(host);
    scheme = parsed.scheme;
    host = parsed.host;
  }

  final segments = <String>[];

  if (usePrefix && _prefix != null && _prefix!.isNotEmpty) {
    segments.addAll(_prefix!.split('/').where((e) => e.isNotEmpty));
  }

  segments.addAll(endpoint.split('/').where((e) => e.isNotEmpty));

  if (pathSeg != null && pathSeg.isNotEmpty) {
    segments.addAll(pathSeg);
  }

  return Uri(
    scheme: scheme,
    host: host,
    pathSegments: segments,
    queryParameters: queryParams,
  );
}