Home > Net >  Other data does not get pushed into database, only image
Other data does not get pushed into database, only image

Time:12-05

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:

  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?

CodePudding user response:

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

await imageref.putFile(selectedImage!);
return await imageref.getDownloadURL();
  • Related