inApp method

Future<Object?> inApp(
  1. BuildContext context
)

Parses and opens uri in Youtube app if possible, else platform handles it.

If uri launching fails and enableFallback is true then opens youtube iframe player in webview_flutter using inWebview. On failures, a snackbar is shown with errorMessage.

Implementation

Future<Object?> inApp(BuildContext context) async {
  final messengerState = ScaffoldMessenger.of(context);
  if (!uri.isValid) {
    onYoutubePlayerError(
      messengerState,
      _errorMessage,
      'Youtube uri "$uri" is invalid',
    );
    return false;
  }

  bool didOpen = false;

  try {
    if (Platform.isAndroid) {
      didOpen = await inAndroidApp();
    } else if (Platform.isIOS) {
      didOpen = await inIOS();
    } else {
      await inWebview(context);
    }

    if (didOpen) {
      return true;
    }
  } catch (e, s) {
    if (!enableFallback && (Platform.isAndroid || Platform.isIOS)) {
      onYoutubePlayerError(
        messengerState,
        _errorMessage,
        'Something went wrong',
        e,
        s,
      );
      return false;
    }
    // open video in webview as fallback if previous methods failed on android or IOS
  }

  try {
    // ignore: use_build_context_synchronously
    return await inWebview(context);
  } catch (e, s) {
    onYoutubePlayerError(
      messengerState,
      _errorMessage,
      'Something went wrong',
      e,
      s,
    );
    return false;
  }
}