I am trying to work out how a user can upload a eg a profile image and have this store in firebase. this is the code I have so far which shows the image picker but I cannot get the path nor have the image uploaded
dynamic _showSelectImageDialog() {
return Platform.isIOS ? _iosBottomSheet() : _androidDialog();
}
Future _iosBottomSheet() async => showCupertinoModalPopup(
context: context,
builder: (context) {
return CupertinoActionSheet(
// title: Text('Add Photo'),
actions: <Widget>[
CupertinoActionSheetAction(
onPressed: () => _upload(ImageSource.camera),
child: const Text('Take Photo'),
),
CupertinoActionSheetAction(
onPressed: () => _upload(ImageSource.gallery),
child: const Text('Choose Photo'),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
);
},
);
_androidDialog() {
showDialog(
context: context,
builder: (context) {
return SimpleDialog(
title: const Text('Add Photo'),
children: <Widget>[
SimpleDialogOption(
onPressed: () => _upload(ImageSource.camera),
child: const Text('Take Photo'),
),
SimpleDialogOption(
onPressed: () => _upload(ImageSource.gallery),
child: const Text('Choose From Gallery'),
),
SimpleDialogOption(
onPressed: () => Navigator.pop(context),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.redAccent,
),
),
),
],
);
},
);
}
// Select and image from the gallery or take a picture with the camera
// Then upload to Firebase Storage
_upload(ImageSource source) async {
var picker = ImagePicker();
PickedFile pickedImage;
try {
pickedImage = (await picker.pickImage(source: source, maxWidth: 1920))
as PickedFile;
File imageFile = File(pickedImage.path);
try {
// Uploading the selected image with some custom meta data
await storageRef
.child('uploads/user/avatar/${widget.user.id}/$imageFile.jpg')
.putFile(imageFile);
print(imageFile);
// Refresh the UI
setState(() {});
} on FirebaseException {
// print(error);
}
} catch (err) {
print(err);
}
Navigator.pop(context);
}
_displayProfileImage() {
// No new profile image
if (_profileImage == null) {
// No existing profile image
if (widget.user.profileImageUrl.isEmpty) {
// Display placeholder
return AssetImage('assets/images/user_placeholder.jpg');
} else {
// User profile image exists
return CachedNetworkImageProvider(widget.user.profileImageUrl);
}
} else {
// New profile image
return FileImage(File(_profileImage.path));
}
}