尝试将图像上传到Firebase存储时,不断出现错误

0md85ypi  于 2023-02-25  发布在  其他
关注(0)|答案(3)|浏览(146)

下面是我的代码:

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);
    }
  }
}

我似乎不能弄清楚为什么代码不上传照片,我已经改变了规则在firebase,但无济于事,该文件夹被称为测试,所以如果有人可以建议我做什么或如何测试我的firebase存储,这将是一个很大的帮助.
当我调用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_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}
    • 更新**

我用上面的代码创建了一个新的flutter应用程序,只有两个按钮,所以看起来可能是我的其他应用程序的依赖性或类似的东西,而不是代码的缺陷。感谢大家谁帮助一旦我弄清楚如何让它工作在我的原始应用程序,我会更新。
Firebase Storage working on one application but not another

9rbhqvlz

9rbhqvlz1#

现在您将ref添加到目标,您创建的引用应如下所示。请尝试以下操作

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

final uploadTask = ref.child('$destination/${file.name}.${file.extension}')
    .putFile(file, SettableMetadata(
    contentType: "image/jpeg",
  ));
    • 编辑1***

从这里消除静电。

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);
    }
  }
}

当您希望上传时,请先进行引用

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

这应按预期工作

tyg4sfes

tyg4sfes2#

我有类似的问题,它解决这样.

await ref.putData(await file.readAsBytes());
xzlaal3s

xzlaal3s3#

enter code here

最终的防火墙存储=防火墙存储示例引用();
//最新变量用户= _auth.当前用户;
变量postid =用户标识符().v1();
将来的postData(imageFile)异步{

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(),

“媒体”:网址,

});
return Iurl;

}

相关问题