skip to Main Content

Here is my code:

 Future selectFile() async {
    final result =
        await ImagePicker.platform.pickImage(source: ImageSource.gallery);

    if (result == null) return;
    final path = result.path;

    setState(() {
      file = File(path);
    });
  }

  Future uploadFile() async {
    if (file == null) return;

    final fileName = file!.path.split("/").last;
    final destination = "test/";

    Storage.uploadFile(destination, file!);
  }
import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';

class Storage {
  static Future<void> uploadFile(String destination, File file) async {
    try {
      final ref = FirebaseStorage.instance.ref().child(destination);

      await ref.putFile(file);
    } on FirebaseException catch (e) {
      print(e);
    }
  }
}

I can’t seem to work out why exactly the code does not upload a photo, I have changed the rules in firebase but to no avail and the folder is called test so if anyone could suggest what I do or how to test my firebase storage, that would be a great help. Thanks in advance.

I keep getting this error when I call uploadFile:

E/StorageException(27972):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
E/StorageException(27972):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
E/StorageException(27972):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
E/StorageException(27972):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.sendWithRetry(UploadTask.java:477)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.beginResumableUpload(UploadTask.java:276)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.run(UploadTask.java:224)
E/StorageException(27972):  ... 5 more
I/flutter (27972): [firebase_storage/object-not-found] No object exists at the desired reference.

Firebase rules are:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

UPDATE

I created a new flutter application with the above code and just two buttons, so it seems it may be a flaw in my other application’s dependencies or something like that rather than the code. Thank you to everyone who helped once I figure out how to get it to work on my original application I will update.

Firebase Storage working on one application but not another

3

Answers


  1. I have the similar problem and it solve like this.

    await ref.putData(await file.readAsBytes());
    
    Login or Signup to reply.
  2. The reference you created should look like this now you added ref to the destination. please try the following

    final ref = FirebaseStorage.instance.ref().child('$destination/${file.name}.${file.extension}');
    

    EDIT

    final ref = FirebaseStorage.instance.ref();
    
    
    final uploadTask = ref.child('$destination/${file.name}.${file.extension}')
        .putFile(file, SettableMetadata(
        contentType: "image/jpeg",
      ));
    

    Edit1*

    Remove static from here.

    import 'dart:io';
    
    import 'package:firebase_storage/firebase_storage.dart';
    
    class Storage {
      Future<void> uploadFile(String destination, File file) async {
        try {
          final ref = FirebaseStorage.instance.ref().child(destination);
    
          await ref.putFile(file);
        } on FirebaseException catch (e) {
          print(e);
        }
      }
    }
    

    When you wish to upload first make a reference

    Storage _storage = Storage();
    _storage.uploadFile(destination, file!);
      
    

    This should work as expected

    Login or Signup to reply.
  3. enter code here
    

    final firebaseStorage = FirebaseStorage.instance.ref();

    // late var user = _auth.currentUser;

    var postid = Uuid().v1();

    Future postData( imageFile) async {

    enter code here
    print("******************** ${timeles}");
    final user = await _auth.currentUser;
    enter code here
    UploadTask uploadTask =
        firebaseStorage.child("post_$postid.jpg").putFile(imageFile) ;
    TaskSnapshot f = await uploadTask.timeout(Duration(seconds: 50));
    TaskSnapshot taskSnapshot = await uploadTask.timeout(Duration(seconds: 20));
    enter code here
    String Iurl =( await taskSnapshot.ref.getDownloadURL()).toString();
    
    
    enter code here
    postref.doc(user?.uid).collection("posts").doc(postid).set({
      "postid": postid,
      "ownerid": user?.uid,
      "email": user?.email,
      "price": _price.text.trim(),
      "phone": _phoneControlle.text.trim(),
      "name": _name.text.trim(),
      "location": _title.text.trim(),
      "dateprice": timeles.toString(),
    

    "mediaurl": Iurl,

    });
    return Iurl;
    

    }

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