Firebase技术求助:'StorageReference'类型无'put'成员报错
Hey there! That error pops up because you’re using a newer version of the Firebase Storage SDK (v9.0+), which uses a modular API, while the YouTube tutorial you followed relies on the older namespaced API where put() was a valid method. Let’s get this sorted out quickly.
Why this happens
Firebase 9.0+ completely revamped its SDKs to be tree-shakable and modular. For Storage, the old put() method was split into more specific methods depending on what you’re uploading:
putData()for uploading rawData(like resized image data)putFile()for uploading a local file URL
Step-by-step fix
1. Check your SDK version
First, confirm you’re on Firebase v9.0+ by checking your dependency file:
- iOS (Swift): Look in your
Podfile— if you seepod 'Firebase/Storage'without a version pin, you’re on the latest modular SDK. - Android (Kotlin/Java): Check your
build.gradle— if you havecom.google.firebase:firebase-storage:20.0.0or higher, you’re on the modular SDK.
2. Update your upload code
Here’s how to replace the old put() call with the new API:
Example for iOS (Swift)
If your original tutorial code looked like this:
let storageRef = Storage.storage().reference().child("avatars/\(user.uid)") storageRef.put(imageData, metadata: nil) { metadata, error in // Handle upload result }
Change it to use putData() instead:
import FirebaseStorage let storageRef = Storage.storage().reference().child("avatars/\(user.uid)") // Upload image data storageRef.putData(imageData, metadata: nil) { metadata, error in guard error == nil else { print("Upload failed: \(error!.localizedDescription)") return } // Get the download URL after successful upload storageRef.downloadURL { url, error in guard let downloadUrl = url else { print("Failed to get download URL: \(error!.localizedDescription)") return } // Save this URL to Firestore/Realtime Database for the user's profile } }
Example for Android (Kotlin)
For Android, the modular SDK still uses putBytes() and putFile(), but double-check you’re using the correct method for your data type:
import com.google.firebase.storage.FirebaseStorage import com.google.firebase.storage.StorageReference val storageRef = FirebaseStorage.getInstance().reference.child("avatars/${user.uid}") // Upload byte array storageRef.putBytes(imageByteArray) .addOnSuccessListener { taskSnapshot -> // Get download URL storageRef.downloadUrl.addOnSuccessListener { uri -> val downloadUrl = uri.toString() // Save URL to user profile } } .addOnFailureListener { error -> print("Upload failed: ${error.message}") }
3. Alternative: Downgrade to the old SDK (not recommended)
If you want to stick with the tutorial’s code without changes, you can pin your Firebase Storage version to the last non-modular release (v8.x):
- iOS: Update your
Podfileto:
Then runpod 'Firebase/Storage', '~> 8.10.0'pod installin your terminal. - Android: Update your
build.gradledependency to:implementation 'com.google.firebase:firebase-storage:8.10.0'
Note: Downgrading isn’t ideal long-term, as you’ll miss out on bug fixes, performance improvements, and new features.
If you share your full code snippet, I can help tailor the fix even more specifically to your implementation!
内容的提问来源于stack exchange,提问作者user9210345

