Firebase 存储资源未在 Flutter 应用程序中下载

Firebase Storage Resource Not Downloading in Flutter App

我正在尝试根据 this documentation 从我的 Firebase 云存储项目下载 .mp3,但按下提升按钮(Android 模拟器)没有任何反应。当我什么都不说时,我什么都没说,我在调用 getApplicationDocumentsDirectory() 后立即添加的打印语句永远不会输出到控制台。这个简单的代码是从 Firebase 文档中逐字记录的,我错过了什么?我在 AndroidManifest 中添加了允许互联网和 WRITE_EXTERNAL_STORAGE.

的权限
  fs.FirebaseStorage storage = fs.FirebaseStorage.instance;

ElevatedButton(
              onPressed: () {
                Future<void> download() async {
                  Directory dir = await getApplicationDocumentsDirectory();
                  print("File downloading to : ${dir}");
                  File file = File(
                      '${dir.path}/track-title');

                  fs.DownloadTask task = storage
                      .ref(
                           'gs://project_name.appspot.com/track-title') 
                            // 'project_name/track-title' as shown in docs also does not work
                      .writeToFile(file);

                  task.snapshotEvents.listen((fs.TaskSnapshot snapshot) {
                    print('Task state: ${snapshot.state}');
                    print(
                        'Progress: ${(snapshot.bytesTransferred / snapshot.totalBytes) * 100} %');
                  }, onError: (e) {
                    // The final snapshot is also available on the task via `.snapshot`,
                    // this can include 2 additional states, `TaskState.error` & `TaskState.canceled`
                    print(task.snapshot);

                    if (e.code == 'permission-denied') {
                      print(
                          'User does not have permission to upload to this reference.');
                    }
                  });
                }
              },
              child: Text("Track Title")),

您的代码定义了一个名为 download 的函数,但实际上您并未在任何地方调用该函数。

要执行您定义的 download 函数中的代码,请在 onPressed 的结束 } 之前添加 download()

ElevatedButton(
  onPressed: () {
    Future<void> download() async {
      ...
        if (e.code == 'permission-denied') {
          print(
              'User does not have permission to upload to this reference.');
        }
      });
    }
    download(); // 
  },
  child: Text("Track Title")),