flutter - 图片网址无法从Firebase存储获取到Firestore数据库

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

我试图将图像存储到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”。

最佳答案

试试这个上传图片/文件并获取下载网址

 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;
  });

关于flutter - 图片网址无法从Firebase存储获取到Firestore数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62249447/

相关文章:

flutter - 如何在 Dart 中设置枚举成员的关联常量值?

flutter - 引发了一个异常:NoSuchMethodError:在null上调用了getter 'the0'

firebase - 在 Firestore 控制台中按 id 查询

google-maps - 官方谷歌地图绘制路线

string - 用于格式化日期和时间的 Dart 函数

dart - 是否可以在图标或 FontAwesomeIcons 等中选择随机图标

firebase - 使用Cloud Firestore在Flutter中获取List <DocumentSnapshots>时没有读取次数

angular - Firestore 事件文档快照监听器中断排序

flutter - 带淡出然后淡入的动画交叉淡入淡出

flutter - 如何在 flutter App 上叠加一个小部件?