1

I have a simple upload to firebase storage function

Future uploadFile() async {
    final path = 'selfies/${photo!.name}';
    final file = File(photo!.path);
    final ref = FirebaseStorage.instance.ref().child(path);
    ref.putFile(file);
  }

I am just wondering what the difference is when I'm calling it in a button i.e.

child: ElevatedButton(
  onPressed: uploadFile,
}

child: ElevatedButton(
  onPressed: () {
    uploadFile();
  },
)

And why does this not work?

child: ElevatedButton(
  onPressed: uploadFile(),
}

I tried googling but can't find relevant results because I'm probably googling the wrong terms.

Joey Leo
  • 17
  • 6
  • 1
    `uploadFile()` immediately calls the `uploadFile()` function. Using `onPressed: uploadFile` passes the `uploadFile` *function* as an argument (which can be called later). `onPressed: () { uploadFile(); }` creates and passes an anonymous function as an argument. – jamesdlin Feb 10 '23 at 07:58

0 Answers0