resolveLocalFont function

Future<FontSource?> resolveLocalFont(
  1. String filename,
  2. FontFormat fontFormat
)

Builds a FontSource pointing at a font file that has been bundled natively by the integrator (Android: android/app/src/main/assets, iOS: added as an Xcode bundle resource and declared in Info.plist under UIAppFonts).

Automatically reports through the onError callback of any constructed EntrustIdv instance (same channel as the rest of the SDK's errors) and returns null if the font file cannot be found in the native bundle for the current platform. Call this after constructing EntrustIdv so its onError callback is already registered.

final idv = EntrustIdv(callbacks: callbacks);
final source = await resolveLocalFont('CustomFont-Regular.ttf', FontFormat.ttf);

Implementation

Future<FontSource?> resolveLocalFont(
  String filename,
  FontFormat fontFormat,
) async {
  // Native returns an empty string for both a missing asset and any other
  // lookup failure; a platform exception or a detached engine surfaces as
  // null. Both mean "not usable".
  String? uri;
  try {
    uri = await const MethodChannel(_ChannelNames.method)
        .invokeMethod<String>(_MethodNames.resolveLocalFontUri, filename);
  } on PlatformException catch (error) {
    debugPrint('[EntrustIdv] resolveLocalFont failed: $error');
  } on MissingPluginException catch (error) {
    debugPrint('[EntrustIdv] resolveLocalFont failed: $error');
  }

  if (uri == null || uri.isEmpty) {
    final error = IdvError(
      uuid: _generateUuid(),
      type: ErrorType.invalidSdkParameter,
      category: ErrorType.invalidSdkParameter.category,
      message: 'resolveLocalFont: font "$filename" was not found in the native '
          'bundle for this platform. Make sure it has been added as a native '
          'bundle resource (Android: android/app/src/main/assets, iOS: Xcode '
          'bundle resource + Info.plist UIAppFonts).',
    );
    if (_registeredOnErrorCallbacks.isEmpty) {
      debugPrint(
        '[EntrustIdv] resolveLocalFont: no EntrustIdv instance is registered '
        'to receive onError — construct EntrustIdv before calling '
        'resolveLocalFont so this error can be reported. ${error.message}',
      );
    }
    for (final onError in _registeredOnErrorCallbacks.toList()) {
      onError(error);
    }
    return null;
  }

  return FontSource(
    location: ResourceLocation(type: ResourceLocationType.local, uri: uri),
    fontFormat: fontFormat,
  );
}