Bunthong K
Bunthong K

Reputation: 134

My function not working even if it meets the condition flutter

I have tried to add a function that allow the user to add a profile picture inside my app.

  late String? profileURL = '';
  late File userProfilePicture;

  ...

  Future pickAndUploadImage() async {
      final ImagePicker _picker = ImagePicker();
      try {
        XFile? image = (await _picker.pickImage(source: ImageSource.gallery));
        print('eee');
        await new Future.delayed(const Duration(seconds: 2));
        userProfilePicture = File(image!.path);
        print(userProfilePicture);
        print(File(image.path));

        if (userProfilePicture != File(image.path)) {
          print('It didnt work sorry');
          Navigator.pop(context);
        } 
        else {
          print('Should be startting to put file in');
          var snapshot = await storage
              .ref()
              .child('userProfiles/$userEmail profile')
              .putFile(userProfilePicture);
          print('file put in!');
          profileURL = await snapshot.ref.getDownloadURL();
        }

        setState(() {
          DatabaseServices(uid: loggedInUser.uid).updateUserPhoto(profileURL!);
          print(profileURL);
        });
      } catch (e) {
        print(e);
      }
    }

The thing is, this line : if (userProfilePicture != File(image.path)) seen not to work. I do not know why, but here is my output :

> flutter: eee  
>flutter: 2 File: '/Users/kongbunthong/Library/Developer/CoreSimulator/Devices/..../tmp/image_picker_3912CA1D-AC60-4C84-823F-EB19B630FD1C-11650-0000061E644AD3FA.jpg'
> flutter: It didnt work sorry

The second line shows that there is 2 file overlapping each other, and doesn't that means the 2 files are the same?

And if it is the same, it should print out Should be starting to put the file in. But instead, it prints out: It didn't work sorry.

Any help is VERY MUCH appreciated!

Upvotes: 0

Views: 47

Answers (1)

Victor Eronmosele
Victor Eronmosele

Reputation: 7716

The two files are different File objects and so the equality check will fail.

You can check if the two File paths are the same like this:

if (userProfilePicture.path != image.path)

Upvotes: 1

Related Questions