openDocumentFile function

Future<DocumentOpenResult> openDocumentFile(
  1. File file, {
  2. Future<OpenResult> defaultOpener(
    1. String path, {
    2. String? type,
    })?,
  3. Future<void> chooserOpener(
    1. String path,
    2. String mimeType,
    3. String title
    )?,
})

Implementation

Future<DocumentOpenResult> openDocumentFile(
  File file, {
  Future<OpenResult> Function(String path, {String? type})? defaultOpener,
  Future<void> Function(String path, String mimeType, String title)?
      chooserOpener,
}) async {
  final path = file.path;
  final mimeType = mimeTypeForDocumentPath(path);
  final opener = defaultOpener ?? OpenFile.open;

  if (shouldForceDocumentOpenChooser(path)) {
    try {
      final chooser = chooserOpener ?? _openWithAndroidChooser;
      await chooser(
        path,
        mimeType ?? 'application/octet-stream',
        'Dokument öffnen mit',
      );
      return const DocumentOpenResult(
        DocumentOpenMode.chooser,
        'Dokumentauswahl geöffnet.',
      );
    } on MissingPluginException {
      final result = await opener(path, type: mimeType);
      return _resultFromOpenFile(result);
    } on PlatformException catch (error) {
      if (error.code == 'NO_APP') {
        return const DocumentOpenResult(
          DocumentOpenMode.noApp,
          'Keine App zum Öffnen des Dokuments gefunden.',
        );
      }
      rethrow;
    }
  }

  final result = await opener(path, type: mimeType);
  return _resultFromOpenFile(result);
}