To pick image(gallery,camera.. etc) first you can use image Picker package
then get image as file like this
//Get the file from the image picker and store it
final pickedFile = await picker.getImage(source: ImageSource.camera);
File image;
if (pickedFile != null) {
image = File(pickedFile.path);
} else {
print('No image selected.');
return;
}
then detect what reference you want to save this file image
FirebaseStorage storage = FirebaseStorage.instance;
//Create a reference to the location you want to upload to in firebase
StorageReference reference = storage.ref().child("profileImages/myImageUid");
finally start upload and wait until complete then get URL image
//Upload the file to firebase
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
// Waits till the file is uploaded then stores the download url
String url = await taskSnapshot.ref.getDownloadURL();
Full Code
FirebaseStorage storage = FirebaseStorage.instance;
File image;
try {
//Get the file from the image picker and store it
image = await ImagePicker.pickImage(source: ImageSource.gallery);
} on PlatformException catch (e) {
//PlatformException is thrown with code : this happen when user back with don't
//selected image or not approve permission so stop method here
// check e.code to know what type is happen
return;
}
//Create a reference to the location you want to upload to in firebase
StorageReference reference =
storage.ref().child("profileImages/${user.id}");
//Upload the file to firebase
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
// Waits till the file is uploaded then stores the download url
String url = await taskSnapshot.ref.getDownloadURL();