这是我的代码:
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中的规则,但没有用,文件夹名为test,所以如果有人能建议我做什么或如何测试我的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在一个应用程序上工作,但在另一个上不工作
您创建的引用应该是这样的,现在您向目标添加了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!);
这应该像预期的一样工作
我也有类似的问题,它是这样解决的。
await ref.putData(await file.readAsBytes());
enter code here
final firebaseStorage=firebaseStorage.instance.ref((;
//late var user=_auth.currentUser;
var postid=Uuid((.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(),
"mediaurl":Iurl,
});
return Iurl;
}