首页 > 解决方案 > 即使满足条件,我的功能也无法正常工作

问题描述

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

  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))seen 不起作用。我不知道为什么,但这是我的输出:

> 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.

很感谢任何形式的帮助!

标签: firebaseflutterdartgoogle-cloud-firestorefirebase-storage

解决方案


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

您可以File像这样检查两条路径是否相同:

if (userProfilePicture.path != image.path)

推荐阅读