Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Other data does not get pushed into database, only image

Below, I have a function that seeks to add a Destination into my Realtime Database after uploading image into Firebase Storage.

Upload image and return URL:

  Future uploadDestinationImage(String name) async {
    final imageref = FirebaseStorage.instance
        .ref()
        .child('destinations')
        .child(name + '.jpg');

    imageref.putFile(selectedImage!);
    return await imageref.getDownloadURL();
  }

Adding data into database:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  void addDestination(String name, String details, var category) async {
    if (category == 'Destination') {
      uploadDestinationImage(name).then((value) {
        String desturl = value;
        String key = destdatabaseref.child('Destinations').push().key;
        destdatabaseref.child('Destinations').child(key).set({
          'id': key,
          'name': name,
          'description': details,
          'category': category,
          'photoURL': desturl
        });
      });
    }
  }

However, the code only uploads image into Firebase Storage and doesn’t push data to my Realtime Database. Is there an error somewhere? And is there another way for me to return downloadURL of image and store it into database?

>Solution :

The putFile call is asynchronous, so you need to await that too:

await imageref.putFile(selectedImage!);
return await imageref.getDownloadURL();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading