【问题标题】:How to upload image files to FIRESTORE using crud method?如何使用 crud 方法将图像文件上传到 FIRESTORE?
【发布时间】:2020-09-20 15:29:53
【问题描述】:

我目前正在将一些数据上传到 Firestore:

 Map<String, dynamic> ads = {
                      'adTitle': widget.title,
                      'adPrice': widget.price,
                      'adCategory': widget.dropdownValue,
                      'adCondition': this.newCondtionVal,
                      'adDesc': widget.desc,
                      'user_id': uID,
                    
                      ///TODO upload the images.
                    };
                    crudObj
                        .addData(ads)
                        .then((value) => {
                              showInSnackBar("dataAdded"),
                            })
                        .catchError((e) {
                      print(e);
                    });

通过 Image_Picker 选择的图像,问题是如何使用上述方法将其上传到 firebase。
PS:我是 Flutter 和 Firebase 的新手。

【问题讨论】:

  • 您几乎肯定不想将 Firestore 用于图像数据。 Firestore 文档的大小限制为 1MB,任何许多图像都可以轻松增长到大于该大小。云存储更适合图片。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

您可以将您的图像转换为 base64 并提供给 Firebase,它会为您提供 getDownloadURL。 getDownloadURL 应该保存在某个地方,以便您可以访问它并将其显示到您的 Flutter 应用程序中。

我有一个代码sn-p,你可以参考一下

Future uploadFile() async {    
   StorageReference storageReference = FirebaseStorage.instance    
       .ref()    
       .child('chats/${Path.basename(_image.path)}}');    
   StorageUploadTask uploadTask = storageReference.putFile(_image);    
   await uploadTask.onComplete;    
   print('File Uploaded');    
   storageReference.getDownloadURL().then((fileURL) {    
     setState(() {    
       _uploadedFileURL = fileURL;    
     });    
   });    
 }  

这是关于 Firebase 实时数据库的。如果您需要 firebase 数据库,请告诉我。

【讨论】:

  • 我也分享了一些链接,以便您了解如何操作。 medium.com/firebase-tips-tricks/… 和 c-sharpcorner.com/article/…
  • 如何将此图像文件与我已上传的详细信息链接? (图像将落入存储,细节(字符串)将落入firestore)
  • 制作一个文档并存储这个字符串,你将从getDownloadURL对象中得到这个字符串以及一些细节,如标题、价格等。使用firebase的setData方法
  • 嘿,你的回答很有用。您能否告诉我如何为 firebase 数据库执行此操作,以及有关如何上传多张图片的任何想法?
猜你喜欢
  • 1970-01-01
  • 2022-10-07
  • 2021-11-23
  • 2021-05-13
  • 2019-12-29
  • 2022-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多