Saya tidak mengerti mengapa saya tidak dapat menautkan dan mengirim URL ke dokumen koleksi cloud firestore gambar diambil dengan benar di Penyimpanan tetapi tidak bisa mendapatkan URL di cloud firestore db di sini adalah kode saya:


  bool isLoading = false;
  var firebaseUser =  FirebaseAuth.instance.currentUser;


  Future uploadImage(BuildContext context) async {
    String fileName = basename(_imageFile.path);
    Reference firebaseStorageRef =
    FirebaseStorage.instance.ref().child('uploads/$fileName');
    UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
    TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
    if (taskSnapshot == null) {
      final String downloadUrl =
      await taskSnapshot.ref.getDownloadURL();
      await FirebaseFirestore.instance.doc(firebaseUser.uid)
          .collection("influncerUser")
          .add({"imageUrl": downloadUrl,});
      setState(() {
        isLoading = true;
      });
    }
  }

2
Emanuel Developer 20 November 2020, 19:18

1 menjawab

Jawaban Terbaik

Pertama, mengapa menggunakan if (taskSnapshot == null) ? Jika taskSnapshot adalah nol, Anda tidak dapat mengambil url unduhan, jadi hapuslah.

Kedua, lebih suka menggunakan onComplete daripada .whenComplete(() => null)

Future uploadImage(BuildContext context) async {
String fileName = basename(_imageFile.path);
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child('uploads/$fileName');
UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);

// use onComplete to await when the doc has been pushed.
TaskSnapshot taskSnapshot = await uploadTask.onComplete;

final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
await FirebaseFirestore.instance.doc(firebaseUser.uid)
    .collection("influncerUser")
    .add({"imageUrl": downloadUrl,});
setState(() => isLoading = true);

}

2
Tristan Bilot 20 November 2020, 17:15