jeudi 18 février 2021

How to check if the imagePath == null and set String=null;

I want to set mediaUrl value to null if the finalimagePath == null else upload the image which is selected.

The following code is working(uploading image) but I am not able to set mediaUrl as null. I tried implementing using if else but its not working maybe because it is of a Type Future.

This is my post preview page where I am pushing all data to firebase :

  Future<String> uploadImage(imageFile) async {
    firebase_storage.UploadTask uploadTask =
        storageRef.child("post_$pollPostId.jpg").putFile(imageFile);
    firebase_storage.TaskSnapshot storageSnap = await uploadTask;

    String downloadurl = await storageSnap.ref.getDownloadURL();

    return downloadurl;
  }

  cratePollPostInFireStore({String mediaUrl, String caption, String category}) {
    pollPostRef.doc(_uid).collection("usersPollPosts").doc(pollPostId).set({
      "pollPostid": pollPostId,
      "ownerId": _uid,
      "username": _username,
      "mediaUrl": mediaUrl,
      "caption": caption,
      "category": category,
      "timespanmp": timestamp,
      "polls": {},
    });
  }

  handleSubmit() async {
    setState(() {
      isUploading = true;
    });

    mediaUrl = await uploadImage(await File(finalimagePath).create());

    cratePollPostInFireStore(
      mediaUrl: mediaUrl,
      caption: captionpass,
      category: captionpass,
    );
  
    setState(() {
      isUploading = false;
    });
  }
}

Please help me out .It seems to be so easy but I am very beginner for flutter .

Thank you :)

Aucun commentaire:

Enregistrer un commentaire