1

Here's my function to upload the 2 files into firebase storage I am not sure if i am using proper exception handling and second of all where to put the await statement to give time for uploading itself.

void _uploadtocloud() async {
    //File? filesong,fileimage; initialized outside the function
    String songname = songName.text.toString();
    String thumbnail = '${songName.text}thumbnail';

    final Reference storageRef =
        FirebaseStorage.instance.ref().child('music/$songname');
    final UploadTask uploadTaskSong = storageRef.putFile(filesong!);
    final Reference storageRefimage =
        FirebaseStorage.instance.ref().child('image/$thumbnail');
    final UploadTask uploadTaskimage = storageRefimage.putFile(fileimage!);
    final TaskSnapshot snapshot1 = await uploadTaskSong;
    final TaskSnapshot snapshot2 = await uploadTaskimage;

    if (snapshot1.state == TaskState.success &&
        snapshot2.state == TaskState.success) {
      await Future.delayed(const Duration(seconds: 10));
//is this the right place to put await statement.
      setState(() {
        uploaded = true;
        songName.clear();
      });
    } else {
      setState(() {
        uploadfaliure = true;
      });
    }
  }

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    refer this https://firebase.flutter.dev/docs/storage/upload-files#upload-from-a-file – Harsh Sureja Apr 06 '23 at 04:55
  • Does this answer your question? [How to upload an image file to Firebase Storage and show it in the Flutter web app](https://stackoverflow.com/questions/74739386/how-to-upload-an-image-file-to-firebase-storage-and-show-it-in-the-flutter-web-a) – Rohit Kharche Apr 12 '23 at 14:09

0 Answers0