4

My code allows to open the file upload window using universal_html.InputElement uploadInput = universal_html.FileUploadInputElement(); web Flutter and select the necessary files to load them into the project. If the user does not select any photo and clicks on close/cancel the window, I want react to this. How can i understand that user close window?



  final completer = Completer<List<String>>();
  universal_html.InputElement uploadInput = universal_html.FileUploadInputElement();
  uploadInput.multiple = true;
  uploadInput.accept = 'image/*';
  uploadInput.click();

  uploadInput.addEventListener('change', (e) async {
    final files = uploadInput.files;
    Iterable<Future<String>> resultsFutures = files.map((file) {
      final reader = universal_html.FileReader();
      reader.readAsDataUrl(file);
      reader.onError.listen((error) => completer.completeError(error));
      return reader.onLoad.first.then((_) => reader.result as String);
    });
    final results = await Future.wait(resultsFutures);
    completer.complete(results);
  });

  universal_html.document.body.append(uploadInput);
  final List<String> images = await completer.future;

  uploadInput.remove();


enter image description here

Sergei Eensalu
  • 377
  • 1
  • 15

1 Answers1

1

A way to manage this kind of event is used in the web implementation of the package file_picker.

Here is a code sample to help you (you can also find the full implementation from the package here):

import 'dart:html' as html;
import 'dart:async';

Future<html.File?> pickFile(String type) async {
  final completer = Completer<List<html.File>?>();
  final input = html.FileUploadInputElement() as html.InputElement;
  input.accept = '$type/*';

  var changeEventTriggered = false;
  void changeEventListener(html.Event e) {
    if (changeEventTriggered) return;
    changeEventTriggered = true;

    final files = input.files!;
    final resultFuture = files.map<Future<html.File>>((file) async {
      final reader = html.FileReader();
      reader.readAsDataUrl(file);
      reader.onError.listen(completer.completeError);
      return file;
    });
    Future.wait(resultFuture).then((results) => completer.complete(results));
  }

  void cancelledEventListener(html.Event e) {
    html.window.removeEventListener('focus', cancelledEventListener);

    // This listener is called before the input changed event,
    // and the `uploadInput.files` value is still null
    // Wait for results from js to dart
    Future.delayed(Duration(milliseconds: 500)).then((value) {
      if (!changeEventTriggered) {
        changeEventTriggered = true;
        completer.complete(null);
      }
    });
  }

  input.onChange.listen(changeEventListener);
  input.addEventListener('change', changeEventListener);

  // Listen focus event for cancelled
  html.window.addEventListener('focus', cancelledEventListener);

  input.click();

  final results = await completer.future;
  if (results == null || results.isEmpty) return null;
  return results.first;
}

The idea is to rely on a listener on the focus event so when you will lose the focus on your file picker window without any data loaded it will complete your future with a null value.

Guillaume Roux
  • 6,352
  • 1
  • 13
  • 38