pickFile method

  1. @override
Future<String?> pickFile(
  1. MediaOptions options,
  2. List<String>? allowedExtensions
)
override

Implementation

@override
Future<String?> pickFile(
    MediaOptions options, List<String>? allowedExtensions) async {
  final completer = Completer<String?>();
  final input = web.document.createElement('input') as web.HTMLInputElement;
  input.type = 'file';
  input.style.display = 'none';
  if (allowedExtensions != null && allowedExtensions.isNotEmpty) {
    input.accept =
        allowedExtensions.map((e) => e.startsWith('.') ? e : '.$e').join(',');
  } else {
    input.accept = '*/*';
  }
  input.addEventListener(
      'change',
      (event) async {
        final files = input.files;
        if (files != null && files.length > 0) {
          final file = files.item(0);
          if (file == null) {
            completer.complete(null);
            return;
          }
          final url = web.URL.createObjectURL(file);
          completer.complete(url);
        } else {
          completer.complete(null);
        }
      } as web.EventListener);
  if (web.document.body != null) {
    web.document.body!.appendChild(input);
  }
  input.click();
  input.remove();
  return completer.future;
}