我對 Dart 非常陌生,并且一般都在編碼。在觀看 YouTube 上的教程后,我生成了此代碼。在大多數情況下,我已經能夠自己解決大部分問題,在這里我覺得我需要一些幫助。我已經撰寫了上傳照片的代碼,但它給出了以下錯誤。請幫助我理解這一點。
I/flutter ( 7512): [firebase_storage/object-not-found] No object exists at the desired reference.
我的代碼在這里:-
Future uploadImagetoFB() async {
if (_image != null) {
try{
String fileName = Path.basename(_image.path);
print('The uploaded file name is: ${fileName}');
final Reference storageReference =
FirebaseStorage.instance.ref().child('profileImages');
_imageUrl = await storageReference.getDownloadURL();
print('The Image Url: $_imageUrl');
} catch (e){
print(e);
}
}
}
Future PickedImage(ImageSource source) async {
try {
final image = await ImagePicker()
.pickImage(source: source, maxWidth: 160, maxHeight: 160);
if (image == null) return;
setState(() {
_image = File(image.path);
});
uploadImagetoFB();
} on PlatformException catch (e) {
print('failed to Upload ');
}
}

uj5u.com熱心網友回復:
要將照片上傳到 Firebase,請使用以下代碼。
這是我設定影像 url 的字串:
String? url;
圖片上傳代碼:
uploadImage() async {
final _firebaseStorage = FirebaseStorage.instance;
final _imagePicker = ImagePicker();
PickedFile image;
//Check Permissions
await Permission.photos.request();
var permissionStatus = await Permission.photos.status;
if (permissionStatus.isGranted) {
//Select Image
image = (await _imagePicker.getImage(source: ImageSource.gallery))!;
var file = File(image.path);
int uploadTimestamp = DateTime.now().millisecondsSinceEpoch;
if (image != (null)) {
Reference ref =
_firebaseStorage.ref().child('profileImages/$uploadTimestamp');
UploadTask uploadTask = ref.putFile(file);
var imageUrl = await (await uploadTask).ref.getDownloadURL();
setState(() {
url = imageUrl.toString();
});
} else {
print('No Image Path Received');
}
} else {
print('Permission not granted. Try Again with permission access');
}
}
Firebase 的結果:
個人資料圖片檔案夾:

上傳圖片:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/454193.html
