无法将图像上载到Firebase存储



我正试图从相机上传图像。我设法从相机中获取了图像,但当用户拾取后,它不会显示在Firebase存储中。你能帮我找出出了什么问题吗

var _instance = FirebaseFirestore.instance;
FirebaseAuth auth_ = FirebaseAuth.instance;
File? image;
String? downloadLink;
Future pickImage() async {
var fileToUpload =
await ImagePicker().pickImage(source: ImageSource.camera);
if (image == null) return;
setState(() {
image = File(fileToUpload!.path);
});
Reference referenceWay = FirebaseStorage.instance
.ref()
.child('profilePics')
.child(auth_.currentUser!.uid)
.child("profilPic.png");
UploadTask uploadTask = referenceWay.putFile(image!);
TaskSnapshot downloadURL = (await uploadTask);
String url = await downloadURL.ref.getDownloadURL();
}

必需的依赖项:

dependencies:
firebase_storage: ^3.0.8
firebase_core: ^0.4.0+9
firebase_analytics: ^5.0.6
image_picker:

检查:

final picker = ImagePicker();
Future pickImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_imageFile = File(pickedFile.path);
});
}

File _imageFile;

Future uploadImageToFirebase(BuildContext context) async {
String fileName = basename(_imageFile.path);
StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child('uploads/$fileName');
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
taskSnapshot.ref.getDownloadURL().then(
(value) => print("Done: $value"),
);
}

适用于iOS:在iOS Info.list 中添加权限

<key>NSCameraUsageDescription</key>
<string>Need to access your camera to capture a photo add and update profile picture.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Need to access your photo library to select a photo add and update profile picture</string>

示例代码示例。

最新更新