-2

Here's the problem I'm having with tauri.

'return' shows you the return value I need, and I know for a fact that writing it this way does not work at all.

'pick_file' is called asynchronously, and I know that message passing seems to work, but is there an easier way to get the value I need.

#[tauri::command]
fn open_file() -> String {
    dialog::FileDialogBuilder::default()
        .add_filter("data", &["json"])
        .pick_file(|path_buf| match path_buf {
            Some(p) => return format!("{}", p.to_str().unwrap()),
            _ => return "".into()
        });
}

1 Answers1

2

First, return in a closure returns from the closure and not from the function that contains it.

The more fundamental issue is that you can't return a String from open_file() if you use FileDialogBuilder::pick_file(). According to the documentation, pick_file() is non-blocking and returns immediately without waiting for the user to pick the file. What you can do in the closure is send the file down a channel, and pick it up elsewhere.

user4815162342
  • 141,790
  • 18
  • 296
  • 355