I’m trying to connect Firebase Storage to my flutter project. But I got an error
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled
Exception: [firebase_storage/channel-error] Unable to establish
connection on channel.
I’ve found that it could be problem with rules because I’ve used firebase auth. But my rules in the storage looks like this:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write : if request.auth !=null;
}
}
}
I’ve also tried :
allow read, write : if true
but it has the same problem
I’ve changed my build.gradle by adding dependencies
implementation("com.google.firebase:firebase-storage:21.0.0")
My Code:
Reference get firebaseStorage => FirebaseStorage.instance.ref();
class FirebaseStorageService extends GetxService{
static Future<String?> getImage(String? imgName) async{
if(imgName == null){
return null;
}
try{
var urlRef = firebaseStorage
.child("images")
.child('$imgName.png');
var imgUrl = await urlRef.getDownloadURL(); //here is a problem
return imgUrl;
}catch (e){
throw e;
return null;
}
}
}
2
Answers
Normally Ai give us this kind of answer
But i have to put some more information for the connection here is my code
check both of them and put it in main screeen hope it will help
Is it Flutter for WEB?
If so, this is most likely a CORS problem:
add file cors.json
insert content below
run: gsutil cors set cors.json gs://<bucket_name>
[
{
"origin": [
"*"
],
"method": [
"GET",
"POST",
"PUT",
"DELETE",
"OPTIONS"
],
"maxAgeSeconds": 3600,
"responseHeader": [
"Content-Type",
"Authorization"
]
}
]