skip to Main Content

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


  1. Normally Ai give us this kind of answer

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MyApp());
    }
    

    But i have to put some more information for the connection here is my code

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      if(Platform.isAndroid) {
        await Firebase.initializeApp(
            options: FirebaseOptions(
                apiKey: 'AIzaSyB9m84p8Y_SY6isFvMaOFWOA2g4xKrb6Nc',
                appId: '1:773723122783:android:b26d30ed175962b3413fea',
                messagingSenderId: '773723122783',
                projectId: 'untitled-38d04')
        );
      }
      Get.put(auth());
      runApp(const MyApp());
    }
    

    check both of them and put it in main screeen hope it will help

    Login or Signup to reply.
  2. Is it Flutter for WEB?

    If so, this is most likely a CORS problem:

    1. add file cors.json

    2. insert content below

    3. run: gsutil cors set cors.json gs://<bucket_name>

      [
      {
      "origin": [
      "*"
      ],
      "method": [
      "GET",
      "POST",
      "PUT",
      "DELETE",
      "OPTIONS"
      ],
      "maxAgeSeconds": 3600,
      "responseHeader": [
      "Content-Type",
      "Authorization"
      ]
      }
      ]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search