firebase - 即使满足条件,我的功能也无法正常工作
问题描述
我试图添加一个功能,允许用户在我的应用程序中添加个人资料图片。
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
.
很感谢任何形式的帮助!
解决方案
这两个文件是不同的File
对象,因此相等性检查将失败。
您可以File
像这样检查两条路径是否相同:
if (userProfilePicture.path != image.path)
推荐阅读
- php - 我正在尝试通过 localhost(XAMPP)在 php 中使用邮件功能发送电子邮件,但是电子邮件没有发送。我如何实现它?
- python - 试图在我的 matplotlib 图/该图的基础数据上获取峰值点标签
- react-native - 我们如何自定义 react-native-cn-richtext-editor 的工具栏以在其上添加按钮
- javascript - Expo Location.startLocationUpdatesAsync 后台服务在真实设备上不起作用(ios,android)
- reactjs - 在没有 webpack-dev-server 的情况下手动设置电子反应
- machine-learning - 文本分类:LSTM 与前馈
- r - 将代码推送到 github 时自动更新 R 文档、NAMESPACE 和DESCRIPTION 文件
- python - 创建熊猫数据框
- python - 如何在具有多项服务的 Google App Engine 上使用 Firebase?
- go - 使用 GORM golang 持久化自定义集数据类型