superlistapp / super_native_extensions

Native drag & drop, clipboard access and context menu.
MIT License
461 stars 84 forks source link

Get file paths of multiple files dropped into DropRegion #408

Open alexvoina opened 3 months ago

alexvoina commented 3 months ago

Hi!

I want to use super_drag_and_drop for a fairly simple use case: dropping a list of files ,or more precisely, file paths into a DropRegion.

onPerformDrop: (event) async {
          // Obtain additional reader information first

          event.session.items.forEach((e) =>
              e.dataReader?.getValue(Formats.fileUri, (value) => print(value)));

              print("finished..");

             }

how can I await for getting the file paths of all the files dropped in the session?

knopp commented 3 months ago

You can use Future.wait to wait for multiple futures at the same time.

alexvoina commented 3 months ago

i've seen the example with Future.wait on the readers mapped into the ReaderInfo objects, which then require a fair amount of code & extensions to get the value. Is all of this necessary?

https://github.com/superlistapp/super_native_extensions/blob/main/super_clipboard/example/lib/widget_for_reader.dart#L38

All I want to do, is to get the file paths into a List, before the call to print("finished..")

knopp commented 3 months ago

No. That code shows detailed information about the underlying dropped data to the user. That's not a common use-case. It does a lot more than you need.

alexvoina commented 3 months ago

thanks for the quick replies. I got it working as i want like this:

extension _ReadValue on DataReader {
  Future<T?> readValue<T extends Object>(ValueFormat<T> format) {
    final c = Completer<T?>();
    final progress = getValue<T>(format, (value) {
      c.complete(value);
    }, onError: (e) {
      c.completeError(e);
    });
    if (progress == null) {
      c.complete(null);
    }
    return c.future;
  }
}

onPerformDrop: (event) async {

          final List<Uri?> fileUris = await Future.wait(event.session.items
              .map((e) => e.dataReader!.readValue(Formats.fileUri)));

          fileUris.where((e) => e != null).forEach((uri) => print(uri));

          print("finished..");

        }

Did i get it right, or is there an easier way?