requestWikiInfo static method

EventHandler? requestWikiInfo(
  1. Landmark landmark, {
  2. required void onComplete(
    1. GemError err,
    2. ExternalInfo? extraInfo
    ),
})

Requests Wikipedia information for landmark.

When available, the platform-side Wikipedia data is returned as an ExternalInfo instance via the onComplete callback. The callback is invoked with a GemError indicating success or the failure reason and the ExternalInfo object on success.

The Landmark needs to have Wikipedia information available; use hasWikiInfo to check before requesting.

Parameters

  • landmark: The landmark for which to request Wikipedia information.
  • onComplete: Callback invoked when the request completes. The callback receives:

Returns

  • EventHandler?: A handler that can be used to cancel the pending request, or null if the request could not be initiated.

Example:

if (!ExternalInfoService.hasWikiInfo(landmark)) {
  print('The landmark does not have Wiki info.');
  return;
}

ExternalInfoService.requestWikiInfo(landmark, onComplete: (err, info) {
  if (err == GemError.success) {
    print('Successfully retrieved Wiki info.');
    // Use the retrieved info as needed
  } else if (info != null) {
    print('Failed to retrieve Wiki info.');
  }
});

Implementation

static EventHandler? requestWikiInfo(
  Landmark landmark, {
  required void Function(GemError err, ExternalInfo? extraInfo) onComplete,
}) {
  final (bool hasWikiInfo, ExternalInfo externalInfo) = _checkWikiInfo(
    landmark,
  );
  if (!hasWikiInfo) {
    onComplete(GemError.invalidInput, null);
    return null;
  }

  final ExternalInfoHandler wikiListener = ExternalInfoHandler(externalInfo);

  wikiListener.registerOnCompleteWithData((
    final int err,
    final String hint,
    final Map<dynamic, dynamic> json,
  ) {
    GemKitPlatform.instance.unregisterEventHandler(wikiListener.id);
    final GemError error = GemErrorExtension.fromCode(err);
    if (error == GemError.success) {
      onComplete(error, externalInfo);
    } else {
      onComplete(error, null);
    }
  });
  GemKitPlatform.instance.registerEventHandler(wikiListener.id, wikiListener);

  objectMethod(
    externalInfo.pointerId,
    'ExternalInfo',
    'requestWikiInfo',
    args: <String, dynamic>{
      'first': landmark.pointerId,
      'second': wikiListener.id,
    },
  );

  return wikiListener;
}