skip to Main Content

in my programme i want to call the same path inside a methode called getPhoto()=>upload(statefulwidget) to other file or statefulwidget

Future getPhoto() async{

FirebaseFirestore fearbase = FirebaseFirestore.instance;

 Reference ref=FirebaseStorage.instance
 .ref()
 .child("${widget.user}/ProfileData")
 .child("Url_$postId");

 await ref.putFile(file!);
downloadUrl=await ref.getDownloadURL();

//  upload image to firestore
var list=[];
await fearbase.collection("users").doc(widget.user)
.collection("PostData").doc(ido)
.set({"PostUrl":downloadUrl,"ownerName":loggedInUser.username,"userId":loggedInUser.uid,"timestemp":postId,"PostId":ido,"like":FieldValue
.arrayUnion(list)})
.whenComplete(() => Fluttertoast.showToast(msg: "Image Uploaded successfully .i."));
// .then((DocumentReference ido) => ido.update({"PostId":ido.id}))

}

more specifically i want to get like field path from the other file

2

Answers


  1. There are multiple ways to do this.

    But the simple one is you can create a class and define this method within the class.

    class Demo {
        static void getPhoto() {
            print("photo");
        }
    }
    

    Then your can access it like

    Demo.getPhoto()
    
    Login or Signup to reply.
  2. You can use callback function to solve this.

    Future<String> getPhoto() async {
      Reference ref = FirebaseStorage.instance
          .ref()
          .child("${widget.user}/ProfileData")
          .child("Url_$postId");
      await ref.putFile(file!);
      return await ref.getDownloadURL();
      //  upload image to firestore
      
      // .then((DocumentReference ido) => ido.update({"PostId":ido.id}))
    }
    
    Future upload(String downloadUrl) async {
      FirebaseFirestore firebase = FirebaseFirestore.instance;
      var list = [];
      await firebase.collection("users").doc(widget.user)
          .collection("PostData").doc(ido)
          .set({
        "PostUrl": downloadUrl,
        "ownerName": loggedInUser.username,
        "userId": loggedInUser.uid,
        "timestemp": postId,
        "PostId": ido,
        "like": FieldValue
            .arrayUnion(list)
      })
          .whenComplete(() => Fluttertoast.showToast(msg: "Image Uploaded successfully .i."));
    }
    

    In usage, you can pass the function as follows

      getPhoto().then(upload);
    

    Or

      final downloadUrl = await getPhoto();
      await upload(downloadUrl);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search