使用 SDKOptions 以编程方式设置 HERE 凭据和缓存路径

Use SDKOptions to Set Programmtically HERE Credentials and Cache Path

我想从 Web 服务获取 HERE 凭据,而不是从 android 清单或 HERE 文档中所述的 info.plist,但此时我收到错误消息。

此处Flutter SDK版本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);
    });
  }
}

发生错误

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)

谢谢。

确保在初始化 HERE SDK 之前初始化 SDKContext。我希望这会在文档中说明,但显然没有。

因此,与其在 创建 SDKNativeEngine 之后调用它 ,不如这样调用它:

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());
}