如何使用 Flutter 存储 Cloud Storage for Firebase 中存储的文件的额外信息

zin*_*rox 2 google-cloud-storage firebase google-cloud-platform flutter

我试图在 Firebase 存储中存储一些文件,当用户访问或下载这些文件时,我想显示有关每个文件的一些额外信息(例如描述、日期等)。文件元数据只能存储名称、大小和内容类型,那么在哪里以及如何存储额外信息?

Ren*_*nec 7

如文档中所述,上传文件时,您可以通过SettableMetadata类添加一些“自定义元数据”,如下所示:

  // Create your custom metadata.
  firebase_storage.SettableMetadata metadata =
      firebase_storage.SettableMetadata(
        cacheControl: 'max-age=60',
        customMetadata: <String, String>{
          'userId': 'ABC123',
        },
      );

  try {
    // Pass metadata to any file upload method e.g putFile.
    await firebase_storage.FirebaseStorage.instance
        .ref('uploads/file-to-upload.png')
        .putFile(file, metadata);
  } on firebase_core.FirebaseException catch (e) {
    // e.g, e.code == 'canceled'
  }
Run Code Online (Sandbox Code Playgroud)