'how to send data to FirebaseFirestore and firebase_storage at once

I am having some difficulties with firebase and flutter. In this code I am trying to create a user, sending an image to fireStore, and putting all the data in firebase firestore. It's creating the user, but for some reason it's either not sending the data to firebase firestore or not putting the image in to the firebase storage. Only one of these functionalities is working but not both together.

When I delete the code:

final imageRef = firebase_storage.FirebaseStorage.instance
    .ref()
    .child("userImage")
    .child(FirebaseAuth.instance.currentUser.uid + ".jpg");

await imageRef.putFile(image);

  "userImage": imageRef.getDownloadURL(), // and deleted this field from the collection

the FirebaseFirestore.instance works fine. and when I delete this code:

FirebaseFirestore.instance
    .collection("User")
    .doc(_authResult.user.uid)
    .set({
  "userName": userName,
  "email": email,
  "password": passWord,
  "userImage": imageRef.getDownloadURL(),
});

the imageRef.putFile works and goes to the storage.

So for some reason they are effecting on each other somehow. How can I make both functionalities work?

Here is the full code:

UserCredential _authResult = await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: passWord);
    setState(() {
      isLoading = true;
    });
    
    final imageRef = firebase_storage.FirebaseStorage.instance
        .ref()
        .child("userImage")
        .child(FirebaseAuth.instance.currentUser.uid + ".jpg");
    
    await imageRef.putFile(image);
    
    FirebaseFirestore.instance
        .collection("User")
        .doc(_authResult.user.uid)
        .set({
      "userName": userName,
      "email": email,
      "password": passWord,
      "userImage": imageRef.getDownloadURL(),
    });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source