【发布时间】:2020-09-28 14:14:05
【问题描述】:
我正在使用下面的代码将flutter应用程序中选择的图像上传到firebase存储,这已成功完成,但我无法将图片的url保存在firestore中,在这种情况下我做错了什么?
您可以在下面的代码中看到存储 url 的尝试。请指导我应该如何纠正它?
Future uploadFile(String uid) async {
StorageReference reference = FirebaseStorage.instance.ref().child('profile_pic/$uid');
StorageUploadTask uploadTask = reference.putFile(avatarImageFile);
StorageTaskSnapshot storageTaskSnapshot;
uploadTask.onComplete.then((value) {
if (value.error == null) {
storageTaskSnapshot = value;
storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
setState(() {
photoUrl = downloadUrl;
});
Firestore.instance
.collection('users')
.document(uid)
.setData({ 'photoUrl': photoUrl}).then((data) async {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Upload success");
}).catchError((err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}, onError: (err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: 'This file is not an image');
});
} else {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: 'This file is not an image');
}
}, onError: (err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}
【问题讨论】:
-
@PeterHaddad 我从这里得到了这个代码,在
settings.dart文件github.com/duytq94/flutter-chat-demo
标签: firebase flutter dart google-cloud-firestore firebase-storage