Xử lý lỗi cho Cloud Storage trên Flutter
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Đôi khi, khi bạn đang tạo một ứng dụng, mọi thứ không diễn ra theo kế hoạch và xảy ra lỗi!
Khi nghi ngờ, hãy bắt ngoại lệ do hàm trả về và xem thông báo lỗi có gì.
final storageRef = FirebaseStorage.instance.ref().child("files/uid");
try {
final listResult = await storageRef.listAll();
} on FirebaseException catch (e) {
// Caught an exception from Firebase.
print("Failed with error '${e.code}': ${e.message}");
}
Xử lý thông báo lỗi
Có một số lý do khiến lỗi có thể xảy ra, bao gồm cả việc tệp không tồn tại, người dùng không có quyền truy cập vào tệp mong muốn hoặc người dùng huỷ tải tệp lên.
Để chẩn đoán đúng vấn đề và xử lý lỗi, sau đây là danh sách đầy đủ tất cả các lỗi mà ứng dụng khách của chúng tôi sẽ gặp phải và cách các lỗi đó xảy ra.
Mã |
Mô tả |
storage/unknown |
Đã xảy ra lỗi không xác định. |
storage/object-not-found |
Không có đối tượng nào tại vị trí tham chiếu mong muốn. |
storage/bucket-not-found |
Không có bộ chứa nào được định cấu hình cho Cloud Storage |
storage/project-not-found |
Không có dự án nào được định cấu hình cho Cloud Storage |
storage/quota-exceeded |
Bạn đã vượt quá hạn mức trên vùng chứa Cloud Storage. Nếu bạn đang dùng gói giá Spark, hãy cân nhắc nâng cấp lên gói giá linh hoạt (trả tiền theo mức dùng). Nếu bạn đang dùng Gói giá linh hoạt, hãy liên hệ với Nhóm hỗ trợ Firebase.
Lưu ý quan trọng: Kể từ ngày 1 tháng 10 năm 2025, bạn bắt buộc phải dùng Gói giá linh hoạt để sử dụng Cloud Storage, ngay cả các vùng lưu trữ mặc định. |
storage/unauthenticated |
Người dùng chưa được xác thực, vui lòng xác thực rồi thử lại. |
storage/unauthorized |
Người dùng không được phép thực hiện hành động mong muốn, hãy kiểm tra các quy tắc bảo mật để đảm bảo chúng chính xác. |
storage/retry-limit-exceeded |
Đã vượt quá giới hạn thời gian tối đa cho một thao tác (tải lên, tải xuống, xoá, v.v.). Hãy thử tải lên lại. |
storage/invalid-checksum |
Tệp trên máy khách không khớp với tổng kiểm của tệp mà máy chủ nhận được. Hãy thử tải lên lại. |
storage/canceled |
Người dùng đã huỷ thao tác. |
storage/invalid-event-name |
Tên sự kiện bạn cung cấp không hợp lệ. Phải là một trong các giá trị [running , progress , pause ] |
storage/invalid-url |
Bạn đã cung cấp URL không hợp lệ cho refFromURL() . Phải có dạng: gs://bucket/object hoặc https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN> |
storage/invalid-argument |
Đối số được truyền đến put() phải là Mảng File , Blob hoặc UInt8 . Đối số được truyền đến putString() phải là một chuỗi thô, Base64 hoặc Base64URL . |
storage/no-default-bucket |
Bạn chưa đặt nhóm nào trong thuộc tính storageBucket của cấu hình. |
storage/cannot-slice-blob |
Thường xảy ra khi tệp cục bộ đã thay đổi (bị xoá, lưu lại, v.v.). Hãy thử tải lên lại sau khi xác minh rằng tệp không thay đổi. |
storage/server-file-wrong-size |
Tệp trên ứng dụng không khớp với kích thước của tệp mà máy chủ nhận được. Hãy thử tải lên lại. |
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-25 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-25 UTC."],[],[],null,["\u003cbr /\u003e\n\nSometimes when you're building an app, things don't go as planned and an\nerror occurs!\n\nWhen in doubt, catch the exception thrown by the function\nand see what the error message has to say. \n\n final storageRef = FirebaseStorage.instance.ref().child(\"files/uid\");\n try {\n final listResult = await storageRef.listAll();\n } on FirebaseException catch (e) {\n // Caught an exception from Firebase.\n print(\"Failed with error '${e.code}': ${e.message}\");\n }\n\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nHandle Error Messages\n\nThere are a number of reasons why errors may occur, including the file\nnot existing, the user not having permission to access the desired file, or the\nuser cancelling the file upload.\n\nTo properly diagnose the issue and handle the error, here is a full list of\nall the errors our client will raise, and how they occurred.\n\n| Code | Description |\n|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `storage/unknown` | An unknown error occurred. |\n| `storage/object-not-found` | No object exists at the desired reference. |\n| `storage/bucket-not-found` | No bucket is configured for Cloud Storage |\n| `storage/project-not-found` | No project is configured for Cloud Storage |\n| `storage/quota-exceeded` | Quota on your Cloud Storage bucket has been exceeded. If you're on the Spark pricing plan, consider upgrading to the [pay-as-you-go Blaze pricing plan](/pricing). If you're already on the Blaze pricing plan, reach out to Firebase Support. **Important** : Starting October 1, 2025, the [Blaze pricing plan will be *required* to use Cloud Storage](/docs/storage/faqs-storage-changes-announced-sept-2024), even default buckets. |\n| `storage/unauthenticated` | User is unauthenticated, please authenticate and try again. |\n| `storage/unauthorized` | User is not authorized to perform the desired action, check your security rules to ensure they are correct. |\n| `storage/retry-limit-exceeded` | The maximum time limit on an operation (upload, download, delete, etc.) has been excceded. Try uploading again. |\n| `storage/invalid-checksum` | File on the client does not match the checksum of the file received by the server. Try uploading again. |\n| `storage/canceled` | User canceled the operation. |\n| `storage/invalid-event-name` | Invalid event name provided. Must be one of \\[`running`, `progress`, `pause`\\] |\n| `storage/invalid-url` | Invalid URL provided to `refFromURL()`. Must be of the form: `gs://bucket/object` or `https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=\u003cTOKEN\u003e` |\n| `storage/invalid-argument` | The argument passed to `put()` must be `File`, `Blob`, or `UInt8` Array. The argument passed to `putString()` must be a raw, `Base64`, or `Base64URL` string. |\n| `storage/no-default-bucket` | No bucket has been set in your config's `storageBucket` property. |\n| `storage/cannot-slice-blob` | Commonly occurs when the local file has changed (deleted, saved again, etc.). Try uploading again after verifying that the file hasn't changed. |\n| `storage/server-file-wrong-size` | File on the client does not match the size of the file received by the server. Try uploading again. |"]]