toFile function

Future<File> toFile(
  1. String uriString
)

Create a File object from a uri.

Don't pass uri parameter using Uri object via uri.toString(). Because uri.toString() changes the string to lowercase which causes this package to misbehave

If you are using uni_links package for deep linking purpose. Pass the uri string using getInitialLink() or linkStream

Supported uri scheme same as supported by File.fromUri(uri) with content uri (Android Only)

If uri cannot reference a file this throws UnsupportedError or IOException

Implementation

Future<File> toFile(String uriString) async {
  Uri uri = Uri.parse(uriString);
  if (Platform.isAndroid && uri.isScheme('content')) {
    try {
      String filepath = await _methodChannel
          .invokeMethod("fromUri", {"uriString": uriString});
      return File(filepath);
    } on PlatformException catch (e) {
      switch (e.code) {
        case _URI_NOT_SUPPORTED:
          {
            throw UnsupportedError(
                'Cannot extract a file path from a ${uri.scheme} URI');
          }
        case _IO_EXCEPTION:
          {
            throw UriIOException(e.message);
          }
        default:
          {
            rethrow;
          }
      }
    } catch (e) {
      rethrow;
    }
  }

  return File.fromUri(uri);
}