init method

Future<void> init()

Fetch base URLs

Implementation

Future<void> init() async {
  if (_initialized) return;

  // 1. Get package name
  await _systemService.init();
  if (_systemService.packageName == null) return;

  // 2. Get URLs
  final res = await _apiService.request(
    method: RequestMethod.post,
    url: '${BaseURL.freeMap}/urls',
    body: {'deviceId': '1', 'appId': _systemService.packageName},
  );

  _initialized = res.success;

  // 3. Update base URLs
  try {
    _freeMapURL = res.data['data']['fm'];
    _logService.logURL = res.data['data']['log'];
    final searchURL = res.data['data']['search'];
    final reverseURL = res.data['data']['reverse'];
    final routingURL = res.data['data']['polyline'];

    // Ensure the custom URL set by the developer is not overridden.
    if ((_searchURL.isCustom && _searchURL.url == searchURL) ||
        (!_searchURL.isCustom && _searchURL.url != searchURL)) {
      _searchURL = UrlModel(url: searchURL);
    }

    if ((_reverseURL.isCustom && _reverseURL.url == reverseURL) ||
        (!_reverseURL.isCustom && _reverseURL.url != reverseURL)) {
      _reverseURL = UrlModel(url: reverseURL);
    }

    if ((_polylineURL.isCustom && _polylineURL.url == routingURL) ||
        (!_polylineURL.isCustom && _polylineURL.url != routingURL)) {
      _polylineURL = UrlModel(url: routingURL);
    }
  } catch (e, st) {
    _logService.logError(e, st);
  }
}