How do you store extra information of files stored in Cloud Storage for Firebase with Flutter

Viewed 296

I'm trying to store some files in Firebase storage and when the user accesses or downloads those files, I want to show some extra information about each file (such as a description, date, etc). File metadata can only store name, size and contentType so where and how can I store the extra information?

2 Answers

As explained in the doc, when uploading a file you can add some "custom metadata" via the SettableMetadata Class, as follows:

  // 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'
  }

You can use custom meta along with file in firebase storage like the other answer, or you could create a firestore document for it. Let me know if you need some sample code

Related