Skip to content

Fix bucket name being reused between instances of different buckets#496

Open
minhdanh wants to merge 3 commits into
Baseflow:developfrom
minhdanh:develop
Open

Fix bucket name being reused between instances of different buckets#496
minhdanh wants to merge 3 commits into
Baseflow:developfrom
minhdanh:develop

Conversation

@minhdanh

@minhdanh minhdanh commented Sep 8, 2025

Copy link
Copy Markdown
Contributor

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

This fixes an issue in which a subsequent FirebaseCacheManager instance cannot load or upload files correctly because the bucket name was set and cached in the first FirebaseCacheManager instance.
It also updates packages to latest versions.

⤵️ What is the current behavior?

In my case when I try to read a file from another bucket I see error " [firebase_storage/object-not-found] No object exists at the desired reference." although the file is there.

🆕 What is the new behavior (if this is a feature change)?

If we're initializing a FirebaseCacheManager with a new instance, it makes sure to use another cache key so that files from the other bucket can be read.

💥 Does this PR introduce a breaking change?

No

🐛 Recommendations for testing

Hmm, not sure

📝 Links to relevant issues/docs

🤔 Checklist before submitting

  • All projects build
  • Follows style guide lines (code style guide)
  • Relevant documentation was updated
  • Rebased onto current develop

@rickdijk rickdijk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that two managers with different buckets get different cache keys / file services.

/// and store them in your local cache.
class FirebaseCacheManager extends CacheManager {
static const key = 'firebaseCache';
static const defaultKey = 'firebaseCache';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keydefaultKey is a public API rename if anyone used FirebaseCacheManager.key. Prefer keeping key or a deprecated alias.

FirebaseCacheManager._(retryOptions: _retryOptions, bucket: _bucket);
static final Map<String, FirebaseCacheManager> _instances = {};

static RetryOptions? _retryOptions;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These statics are no longer needed once each instance owns its own bucket/retryOptions. Keeping them causes cross-instance leakage.

_retryOptions = retryOptions;
return _instance;
final cacheKey = bucket ?? defaultKey;
if (_instances.containsKey(cacheKey)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an instance already exists, a new retryOptions argument is silently ignored. Worth documenting, or only accepting options on first create.

if (_instances.containsKey(cacheKey)) {
return _instances[cacheKey]!;
}
_bucket = bucket ?? _bucket;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_bucket = bucket ?? _bucket (and the same for _retryOptions) leaks state across instances.

FirebaseCacheManager(bucket: 'bucket-a');
FirebaseCacheManager(); // cacheKey is defaultKey, but _bucket is still 'bucket-a'

Pass the factory args directly instead:

final instance = FirebaseCacheManager._(
  key: cacheKey,
  retryOptions: retryOptions,
  bucket: bucket,
);

(or putIfAbsent and drop the statics entirely)

sdk: flutter
flutter_cache_manager: ^3.4.1
firebase_storage: '>=12.0.0 <13.0.0'
firebase_storage: ^13.0.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version bump should preferably be done in a different PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants