![](/img/trans.png)
[英]How to upload image to Firebase Storage and get the DownloadUrl Flutter
[英]How to upload image to Firebase using Flutter
我正在使用 image_picker 库https://pub.dartlang.org/packages/image_picker来选择/拍摄我想上传的照片。 到目前为止,代码成功地将图像上传到 Firebase 存储,唯一的问题是图像上传后应用程序关闭(并没有真正崩溃,它只是关闭并且 VS 代码失去了与设备的连接)。 代码如下:
File _image;
Future _takeProfilePicture() async{
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState((){
_image = image;
});
}
Future _selectProfilePicture() async{
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState((){
_image = image;
});
}
Future<Null> _uploadProfilePicture() async{
FirebaseUser user = await FirebaseAuth.instance.currentUser();
final StorageReference ref = FirebaseStorage.instance.ref().child('${user.email}/${user.email}_profilePicture.jpg');
final StorageUploadTask uploadTask = ref.putFile(_image);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
}
void _selectAndUploadPicture() async{
await _selectProfilePicture();
await _uploadProfilePicture();
}
void _takeAndUploadPicture() async{
await _takeProfilePicture();
await _uploadProfilePicture();
}
终端打印以下内容:
W/Firestore( 6873): (0.6.6-dev) [Firestore]: The behavior for java.util.Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
W/Firestore( 6873): To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
W/Firestore( 6873):
W/Firestore( 6873): FirebaseFirestore firestore = FirebaseFirestore.getInstance();
W/Firestore( 6873): FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
W/Firestore( 6873): .setTimestampsInSnapshotsEnabled(true)
W/Firestore( 6873): .build();
W/Firestore( 6873): firestore.setFirestoreSettings(settings);
W/Firestore( 6873):
W/Firestore( 6873): With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timestamp. For example:
W/Firestore( 6873):
W/Firestore( 6873): // Old:
W/Firestore( 6873): java.util.Date date = snapshot.getDate("created_at");
W/Firestore( 6873): // New:
W/Firestore( 6873): Timestamp timestamp = snapshot.getTimestamp("created_at");
W/Firestore( 6873): java.util.Date date = timestamp.toDate();
W/Firestore( 6873):
W/Firestore( 6873): Please audit all existing usages of java.util.Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.
我尝试从终端实现建议的 java 代码,但我似乎无法找到一种方法来使用 cloud_firestore 库https://pub.dartlang.org/packages/cloud_firestore在 flutter 中编写等价物,它没有 FirebaseFirestoreSettings 的等价物(或者我似乎找不到一个)。 有没有解决的办法?
先感谢您!
更新版本的 mmccabe 答案适用于 firebase_storage 插件的1.0.4
版
Future<String> _pickSaveImage(String imageId) async {
File imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
StorageReference ref =
FirebaseStorage.instance.ref().child(imageId).child("image.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
return await (await uploadTask.onComplete).ref.getDownloadURL();
}
截至目前,您必须执行以下操作:
var downloadURL = (await uploadTask.onComplete).ref.getDownloadURL();
因为错误:未为类“StorageUploadTask”定义 getter 'future'。 (undefined_getter 在 [timetracker] lib/screens/image_detection.dart:63)
以下对我有用:
Future<Uri> _pickSaveImage(String imageId) async {
File imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
StorageReference ref =
FirebaseStorage.instance.ref().child(imageId).child("image.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
return (await uploadTask.future).downloadUrl;
}
这对我有用......就我而言,我还需要获取下载 URL 并推送到 Firestore 集合。 在做了一些阅读后,我发现最好使用 StorageTaskSnapshot 然后获取下载 URL
uploadImage(File image) async {
StorageReference reference =
FirebaseStorage.instance.ref().child(image.path.toString());
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
String url = (await downloadUrl.ref.getDownloadURL());
}
Future<String> _pickSaveImage(String imageId) async {
File imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
StorageReference ref =
FirebaseStorage.instance.ref().child(imageId).child("image.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
return await (await uploadTask.onComplete).ref.getDownloadURL();
}
截至目前,
这给出了一个错误:
return await (await uploadTask.onComplete).ref.getDownloadURL();
这不会:
return (await uploadTask.onComplete).ref.getDownloadURL();
错误信息:
The getter 'future' isn't defined for the class 'StorageUploadTask'.
undefined_getter at [timetracker] lib/screens/image_detection.dart:63)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.