Search code examples
androidiosflutterdartpersonalization

Flutter, Nothing Happens When Trying to Change Profile Picture


I have a function where users click on a button and the gallery will open for users to choose a picture as their profile picture.

There was no error anywhere so I don't know what is wrong, I'm pretty sure I called the function correctly.

Full code below:

profile.dart

TextButton(
                      onPressed: () => controller.uploadUserProfilePicture,
                      child: Text(
                        'Change Profile Picture',
                        style: TextStyle(
                          color: Colors.grey[600],
                        ),
                      ),
                    ),

user_controller.dart

// upload profile image
  uploadUserProfilePicture() async {
    try {
      final ImagePicker picker = ImagePicker();
      // Pick an image
      final XFile? image = await picker.pickImage(
        source: ImageSource.gallery,
        imageQuality: 70,
        maxHeight: 512,
        maxWidth: 512,
      );
      if (image != null) {
        imageUploading.value = true;
        // upload image
        final imageUrl =
            await userRepository.uploadImage('Users/Images/Profile/', image);

        // update user image record
        Map<String, dynamic> json = {'ProfilePicture': imageUrl};
        await userRepository.updateSingleField(json);

        user.value.profilePicture = imageUrl;
        user.refresh();

        NLoaders.successSnackBar(
            title: 'Congratulations!',
            message: 'Your Profile Image has been updated!');
      }
    } catch (e) {
      NLoaders.errorSnackBar(title: 'Oh Snap!', message: e.toString());
    } finally {
      imageUploading.value = false;
    }
  }

Solution

  • in profile.dart just add () after controller.uploadUserProfilePicture because you are referencing the function without invoking it.

    TextButton(   
      onPressed: () => controller.uploadUserProfilePicture(),
     ),