Skip to content

Commit 1f1fb83

Browse files
committed
feat(storage): allow user to delete custom metadata to match underlying SDKs behavior
1 parent 74b9a05 commit 1f1fb83

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

packages/firebase_storage/firebase_storage_platform_interface/lib/src/settable_metadata.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SettableMetadata {
4141
final String? contentType;
4242

4343
/// Additional user-defined custom metadata.
44-
final Map<String, String>? customMetadata;
44+
final Map<String, String?>? customMetadata;
4545

4646
/// Returns the settable metadata as a [Map].
4747
Map<String, dynamic> asMap() {

packages/firebase_storage/firebase_storage_web/lib/src/utils/metadata.dart

+14-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,26 @@ storage_interop.SettableMetadata settableMetadataToFbSettableMetadata(
4848
storage_interop.UploadMetadata settableMetadataToFbUploadMetadata(
4949
SettableMetadata metadata,
5050
{String? md5Hash}) {
51+
// We are removing null values from the customMetadata map.
52+
// since we are setting the whole map, whereas in Android and iOS we can
53+
// set individual keys.
54+
final listOfCustomMetadata = metadata.customMetadata?.entries
55+
.where((entry) => entry.value != null)
56+
.map((entry) => MapEntry(entry.key, entry.value))
57+
.cast<MapEntry<String, String>>();
58+
59+
final Map<String, String>? filteredCustomMetadata =
60+
listOfCustomMetadata != null
61+
? Map.fromEntries(listOfCustomMetadata)
62+
: null;
63+
5164
return storage_interop.UploadMetadata(
5265
cacheControl: metadata.cacheControl,
5366
contentDisposition: metadata.contentDisposition,
5467
contentEncoding: metadata.contentEncoding,
5568
contentLanguage: metadata.contentLanguage,
5669
contentType: metadata.contentType,
57-
customMetadata: metadata.customMetadata,
70+
customMetadata: filteredCustomMetadata,
5871
md5Hash: md5Hash,
5972
);
6073
}

packages/firebase_storage/firebase_storage_web/lib/src/utils/metadata_cache.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SettableMetadataCache {
3131
return _cache;
3232
}
3333

34-
final newMetadata = <String, String>{
34+
final newMetadata = <String, String?>{
3535
...?incoming.customMetadata,
3636
...?_cache.customMetadata,
3737
};

tests/integration_test/firebase_storage/reference_e2e.dart

+21
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,27 @@ void setupReferenceTests() {
391391
expect(fullMetadata.customMetadata!['foo'], 'bar');
392392
});
393393

394+
test('delete metadata', () async {
395+
Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
396+
FullMetadata fullMetadata = await ref
397+
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar'}));
398+
expect(fullMetadata.customMetadata!['foo'], 'bar');
399+
400+
fullMetadata = await ref.updateMetadata(
401+
SettableMetadata(
402+
customMetadata: {
403+
'foo': null,
404+
},
405+
),
406+
);
407+
expect(fullMetadata.customMetadata!['foo'], isNull);
408+
409+
// Setting it again
410+
fullMetadata = await ref
411+
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar2'}));
412+
expect(fullMetadata.customMetadata!['foo'], 'bar2');
413+
});
414+
394415
test('errors if property does not exist', () async {
395416
Reference ref = storage.ref('flutter-tests/iDoNotExist.jpeg');
396417

0 commit comments

Comments
 (0)