如何在Flutter中的Firestore中修复“图片上传”中的“空”值

时间:2019-08-13 00:24:52

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

我正在尝试获取从Firebase存储上载到Cloud Firestore的图像的下载链接。

成功打印了img的URL,但是在firebase控制台中,它给了我一个空值。

我尝试过多次实现此图片上传,希望能有所帮助。

//Setting url

String url;

//Selecting image from Image Picker

Future _getImage() async {
 var selectedImage =
 await ImagePicker.pickImage(source: ImageSource.gallery);
 setState(() {_image = selectedImage;});
 }

//Uploading image on FireStore

Future<void> uploadPic() async {
 String filename = basename(_image.path);
 final Storage Reference refer = 
 FirebaseStorage.instance.ref().child('profileImages').child(filename);
 final StorageUploadTask uploadTask = refer.putFile(_image);
 var downloadUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
 final url = downloadUrl.toString();
 print(url.toString());
 return url;
 }

//Setting image: in firestore to url

Future<void> saveUser(String email, String uid) async {
 Firestore.instance.runTransaction((Transaction transaction) async {
  DocumentReference reference =
 Firestore.instance.collection('users').document(uid);
  await reference.setData({
    "email": email,
    "name": _name,
    "username": _username,
    "mobile": _mobile,
    "createdDate": DateTime.now(),
    "image": url,
      }).catchError((e) {
    print(e);
     });
   });
 }

//onPressed
 uploadPic();

Output showing print of downloadUrl Showing null value in Firestore

0 个答案:

没有答案