【发布时间】:2021-10-27 22:28:18
【问题描述】:
我尝试添加一个功能,允许用户在我的应用中添加个人资料图片。
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。
非常感谢任何帮助!
【问题讨论】:
-
userProfilePicture != File(image.path)将永远为真。File继承了默认的Object.operator ==实现,它检查对象身份,并且现有对象永远不会等于新构造的对象。你应该比较userProfilePicture.absolute.path != File(image).absolute.path。 (或者只是userProfilePicture.path != image.path,如果你能保证两者都已经是绝对路径。)
标签: firebase flutter dart google-cloud-firestore firebase-storage