即使满足条件,我的功能也无法正常工作

My function not working even if it meets the condition flutter

我尝试添加一个允许用户在我的应用程序中添加个人资料图片的功能。

  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);
      }
    }

问题是,这一行:if (userProfilePicture != File(image.path)) 看来不起作用。我不知道为什么,但这是我的输出:

> 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

第二行显示有2个文件相互重叠,这不就说明这2个文件是一样的吗?

如果相同,应该打印出Should be starting to put the file in。但相反,它打印出:It didn't work sorry.

非常感谢任何帮助!

这两个文件是不同的 File 对象,因此相等性检查将失败。

您可以检查两个 File 路径是否相同,如下所示:

if (userProfilePicture.path != image.path)