【问题标题】:image url can't be get from firebase storage to firestore database图像 url 无法从 firebase 存储获取到 firestore 数据库
【发布时间】:2020-09-26 16:09:48
【问题描述】:

我尝试将图像存储到 Firebase 存储并尝试获取数据库的图像 URL,这些是代码。 这是我用来将图像上传到 Firebase 存储的代码。

   uploadNotice()async{

   String fileName=basename(noticepic.path);
  final StorageReference firebaseStorageRef=FirebaseStorage.instance
  .ref().child('cisnotices/$fileName');
    StorageUploadTask task = firebaseStorageRef.putFile(noticepic);
    var dowurl = await (await task.onComplete).ref.getDownloadURL();
    var url=dowurl.toString();
    noticeProvider.changePhotoUrl(url).then((val){
        print('photo url got');
      }).catchError((e){
        print(e);
      });



}
NoticeProvider noticeProvider=NoticeProvider();

这是我用来更改图片网址的noticeprovider.dart

class NoticeProvider with ChangeNotifier {
  final firestoreService=FirestoreService();

 String _noticeId;
 String _userName;
 String _userPost;
 String _date; 
 String _photoUrl;
 var uuid=Uuid();

 //getters

 String get userName => _userName;
 String get userPost => _userPost;
 String get date => _date;
 String get photoUrl => _photoUrl;

 //setters

  changeUserName(String value){
  _userName=value;
 notifyListeners();
 }
  changeUserPost(String value){
 _userPost=value;
  notifyListeners();
 }
  changeDate(String value){
  _date=value;
  notifyListeners();
}
 changePhotoUrl(String value){
  _photoUrl=value;
  notifyListeners();
}

 saveNotice(){
  var newNotice=Notice(userName: userName,userPost: userPost,photoUrl: photoUrl,
  date: date,noticeId: uuid.v4());
  firestoreService.saveNotice(newNotice);


 }



 } 

这里是firestoreservice.dart

  class FirestoreService {
 Firestore _db=Firestore.instance;

  Future<void> saveNotice(Notice notice){
     return _db.collection('notices').document(notice.noticeId).setData(notice.toMap());
  }

  }

图像保存在 Firebase 存储中,但 URL 无法访问数据库。它总是显示'null'。

【问题讨论】:

  • 很可能在您保存通知时尚未确定下载 URL。第一个 uploadNotice sn-p 和其他 sn-ps 有什么关系?我在这里没有看到对saveNotice 的任何呼叫。
  • 当用户按下上传按钮上传表单的所有详细信息时,我调用了 saveNotice。除 photoUrl 外,所有其他详细信息都出现在数据库中

标签: flutter dart google-cloud-firestore firebase-storage


【解决方案1】:

试试这个上传图片/文件并获取它的下载 URL

 final StorageReference storageReferencem = FirebaseStorage()
      .ref()
      .child("Users/${DateTime.now().millisecondsSinceEpoch}");
  final StorageUploadTask uploadTaskm =
  storageReferencem.putFile(croppedFile);
  await uploadTaskm.onComplete;
  await storageReferencem.getDownloadURL().then((url) {
    URL=url;
  });

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 2021-02-27
    • 2021-10-14
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2020-07-15
    相关资源
    最近更新 更多