Flutter FilePicker 没有回馈文件

Flutter FilePicker is not giving back the file

提问人:Sangio 提问时间:7/24/2023 最后编辑:Sangio 更新时间:7/24/2023 访问量:164

问:

我有一个问题,不明白为什么会这样。 当我使用 FilePicker 选择文件时,await 似乎永无止境,代码无法克服“await FilePicker.platform.pickFiles”

代码如下:

  Future<void> _addFile([String name, String uri]) async {
    PlatformFile file;
    FilePickerResult result;
    if (name != null && uri != null) {
      addAttachment(name, uri);
      return;
    } else {
      globals.logHelper.info("SELECTING THE FILE:");
      try {
        result = await FilePicker.platform.pickFiles(
          type: FileType.any
        );
        globals.logHelper.info("RESULT ================" + result.toString());
      } catch (e){
        globals.logHelper.error(e);
      }
      if (result != null && result.files.isNotEmpty) {
        file = result.files.first;
        globals.logHelper.info("FILE FOUND: " + file.name);
        _addFile(file.name, file.path);
      }
      return;
    }
  }

此函数在按钮内调用:

                      child: ElevatedButton(
                          onPressed: () async {
                            await _addFile();
                          },
                          child: Icon(Icons.file_present, size: 75))),

而且,在我选择文件后,如果我尝试选择一个新文件,我会收到以下错误:

I/flutter (17157): [MethodChannelFilePicker] Platform exception: PlatformException(already_active, File picker is already active, null, null)
E/flutter (17157): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'PlatformException' is not a subtype of type 'String'
E/flutter (17157): #0      _AttachmentListViewWidgetState._addFile (package:smtu/widgets/AttachmentListViewWidget.dart:142:33)
E/flutter (17157): <asynchronous suspension>
E/flutter (17157): #1      _AttachmentListViewWidgetState._addAttachment.<anonymous closure>.<anonymous closure> (package:smtu/widgets/AttachmentListViewWidget.dart:110:29)
E/flutter (17157): <asynchronous suspension>
E/flutter (17157): 

我试图放置一个 Try Catch 来查看是否抛出任何错误,但没有发生。 我已经在网上搜索了解决方案,但似乎我的代码应该没问题。

我想得到文件,更准确地说,我只需要filePath。

我最需要它用于 Android。

编辑1: addAttachment 代码:

void addAttachment(String name, String uri) {
    globals.logHelper.info("Adding a new attachment");
    setState(() {
      _attachments.add(Attachment(name, uri));
    });
  }
Android Flutter 异步 FilePicker

评论

0赞 lucie 7/24/2023
你能提供你的addAttachment方法的内容吗?

答: 暂无答案