问题
I've been following a tutorial on how to create a social app and I followed how to upload a post, but the tutorial didn't cover how to upload a new profile picture. So this is what I've been doing on my own. So far, I can select an image from my gallery and it sits in the photo placeholder, and then I press save, it says it was successful, but then I go to the profile page and there's no photo and when I go back to edit profile there's no photo there either.
I looked at Firebase and saw that the profile photo has been successfully saving in the storage directory. It's just not saving in database with the other user stats like the name, email and bio.
I think String _profileImageUrl = ''
; is taking the URL away and making it "" in Firebase, but if I take that out, I get other errors.
This is a snippet from my EditProfilePage.dart file
class EditProfilePage extends StatefulWidget {
final String currentOnlineUserId;
EditProfilePage({
this.currentOnlineUserId
});
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State<EditProfilePage> {
TextEditingController profileNameTextEditingController = TextEditingController();
TextEditingController bioTextEditingController = TextEditingController();
final _scaffoldGlobalKey = GlobalKey<ScaffoldState>();
bool loading = false;
User user;
File _profileImage;
bool _bioValid = true;
bool _profileNameValid = true;
String photoId = Uuid().v4();
void initState() {
super.initState();
getAndDisplayUserInformation();
}
_handleImageFromGallery() async {
File imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
if(imageFile != null){
setState(() {
this._profileImage = imageFile;
});
}
}
_displayProfileImage() {
//No new profile photo
if (_profileImage == null) {
//No existing profile photo
if (user.profileImageUrl == null) {
//Display placeholder
return CircleAvatar(backgroundColor: Colors.grey,);
} else {
//User profile photo exists
return CachedNetworkImageProvider(user.profileImageUrl);
}
} else {
//New profile photo
return FileImage(_profileImage);
}
}
getAndDisplayUserInformation() async {
setState(() {
loading = true;
});
DocumentSnapshot documentSnapshot = await usersReference.document(widget.currentOnlineUserId).get();
user = User.fromDocument(documentSnapshot);
profileNameTextEditingController.text = user.profileName;
bioTextEditingController.text = user.bio;
setState(() {
loading = false;
});
}
updateUserData() async {
setState(() {
profileNameTextEditingController.text.trim().length < 3 || profileNameTextEditingController.text.isEmpty ? _profileNameValid = false : _profileNameValid = true;
bioTextEditingController.text.trim().length > 150 ? _bioValid = false : _bioValid = true;
});
if(_bioValid && _profileNameValid) {
usersReference.document(widget.currentOnlineUserId).updateData({
"profileName": profileNameTextEditingController.text,
"bio": bioTextEditingController.text,
});
//Update user in database
String _profileImageUrl = '';
Future <String> uploadPhoto(mImageFile) async {
StorageUploadTask mStorageUploadTask = storageReference.child("profile_$photoId.jpg").putFile(mImageFile);
StorageTaskSnapshot storageTaskSnapshot = await mStorageUploadTask.onComplete;
String downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
return downloadUrl;
}
String downloadUrl = await uploadPhoto(_profileImage);
usersReference.document(widget.currentOnlineUserId).updateData({
'profileImageUrl': _profileImageUrl,
});
if (_profileImage == null) {
_profileImageUrl = user.profileImageUrl;
} else {
_profileImageUrl = await StorageService.uploadUserProfileImage(
user.profileImageUrl,
_profileImage,
);
}
SnackBar successSnackBar = SnackBar(content: Text("Profile has been updated successfully!"));
_scaffoldGlobalKey.currentState.showSnackBar(successSnackBar);
}
}
This is from my database_service.dart file
static void updateUser(User user) {
usersReference.document(user.id).updateData({
'profileImageUrl': user.profileImageUrl,
});
}
This is from my storage_service.dart file
static Future<String> uploadUserProfileImage(
String profileImageUrl, File _profileImage) async {
String photoId = Uuid().v4();
File image = await compressImage(photoId, _profileImage);
if (profileImageUrl.isNotEmpty) {
//Update user profile image
RegExp exp = RegExp(r'userProfile_(.*).jpg');
photoId = exp.firstMatch(profileImageUrl)[1];
}
StorageUploadTask uploadTask = storageReference
.child('images/users/userProfile_$photoId.jpg')
.putFile(image);
StorageTaskSnapshot storageSnap = await uploadTask.onComplete;
String downloadUrl = await storageSnap.ref.getDownloadURL();
return downloadUrl;
}
I know some of the code might be redundant, I'm not sure what parts are actually needed and parts that aren't. I have no problem getting rid of pieces I don't need. I just need to get the data to go to the right place in Firebase. Please let me know if there's anymore information needed. Thanks
来源:https://stackoverflow.com/questions/62705162/changing-profile-photo-using-firebase-photo-uploads-successfully-but-the-pictur