Search code examples
flutterhere-apiheremaps

Use SDKOptions to Set Programmtically HERE Credentials and Cache Path


I want to get the HERE credentials from a web service and not from the android manifest or info.plist as stated in the HERE documentation, but at this point I get an error.

Here Flutter SDK version 4.8.0.0

import 'package:flutter/material.dart';
import 'package:here_sdk/core.dart';
import 'package:here_sdk/core.engine.dart';
import 'package:here_sdk/core.errors.dart';
import 'package:here_sdk/mapview.dart';

void main() {
  SDKOptions sdkOptions = SDKOptions.withAccessKeySecretAndCachePath(
      "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET", "");

  SDKNativeEngine sdkNativeEngine;
  try {
    sdkNativeEngine = SDKNativeEngine(sdkOptions);
    SDKNativeEngine.sharedInstance = sdkNativeEngine;
  } on InstantiationException {
    // Handle exception.
  }
  SdkContext.init(IsolateOrigin.main);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'HERE SDK for Flutter - Hello Map!',
      home: HereMap(onMapCreated: _onMapCreated),
    );
  }

  void _onMapCreated(HereMapController hereMapController) {
    hereMapController.mapScene.loadSceneForMapScheme(MapScheme.normalDay,
        (MapError? error) {
      if (error != null) {
        print('Map scene not loaded. MapError: ${error.toString()}');
        return;
      }

      const double distanceToEarthInMeters = 8000;
      hereMapController.camera.lookAtPointWithDistance(
          GeoCoordinates(52.530932, 13.384915), distanceToEarthInMeters);
    });
  }
}

An error occurred

E/flutter (16773): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Invalid argument(s): Failed to resolve an FFI function. Perhaps `LibraryContext.init()` was not called.
E/flutter (16773): Failed to lookup symbol (undefined symbol: here_sdk_sdk_core_engine_SDKOptions_make__String_String_String)
E/flutter (16773): #0      catchArgumentError (package:here_sdk/src/_library_context.dart:39:5)
E/flutter (16773): #1      SDKOptions._withAccessKeySecretAndCachePath (package:here_sdk/src/sdk/core/engine/s_d_k_options.dart:207:49)
E/flutter (16773): #2      new SDKOptions.withAccessKeySecretAndCachePath (package:here_sdk/src/sdk/core/engine/s_d_k_options.dart:94:121)
E/flutter (16773): #3      main (package:here_example/main.dart:8:38)
E/flutter (16773): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter (16773): #5      _rootRun (dart:async/zone.dart:1354:13)

  • How can I implement LibraryContext.init()?
  • Can you share code snippet about that?

Thanks.


Solution

  • Make sure to init the SDKContext before initializing the HERE SDK. I wish this would have been stated in the documentation, but apparently it is not.

    So, instead of calling it after creating the SDKNativeEngine, call it like so:

    void main() {
      SdkContext.init(IsolateOrigin.main);
    
      // Clear the cache occupied by a previous instance.
      await SDKNativeEngine.sharedInstance?.dispose();
    
      SDKOptions sdkOptions = SDKOptions.withAccessKeySecretAndCachePath(
          "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET", "");
    
      SDKNativeEngine sdkNativeEngine;    
      try {
        sdkNativeEngine = SDKNativeEngine(sdkOptions);
        SDKNativeEngine.sharedInstance = sdkNativeEngine;
      } on InstantiationException {
        // Handle exception.
      }
    
      runApp(MyApp());
    }