FIrebase Auth 用户对象的 photoURL 属性是否在多个设备上实时更新?

问题描述 投票:0回答:1

我使用 Firebase 身份验证在 Flutter 中开发了一个应用程序。我将每个用户的个人资料图像存储在 Firebase Storage 中。用户对象的“photoURL”属性指向用户上传的 Firebase 存储中的图像,该图像随后显示在个人资料页面上。问题是,当我在两台设备上以同一用户身份登录并更新一台设备上的个人资料图片时,它不会在第二台设备上更新。我必须退出然后重新登录才能更新。

为什么不实时更新?我是否必须将 photoURL 存储在 Firestore 数据库中并从那里更新?或者我的实现方式还有其他问题吗?因为用户对象的 photoURL 属性应该实时更新。这是获取和上传个人资料图片的代码:

更新用户对象

photoURL
的函数:

Future<void> updateUserProfile(String imageUrl) async {
    final user = FirebaseAuth.instance.currentUser;
    if (user != null) {
      final currentDisplayName = user.displayName;
      await user.updateProfile(photoURL: imageUrl, displayName: currentDisplayName);
    }
    notifyListeners();
  }

更新个人资料图片的按钮:

ElevatedButton(
              onPressed: () async {
                final image = await pickImage(context);
                if (image != null) {
                  final croppedImage = await cropImage(image);
                  final croppedXFile = XFile(croppedImage!.path);
                  final imageUrl = await uploadImage(croppedXFile);
                  await Provider.of<ApplicationState>(context, listen: false).updateUserProfile(imageUrl);
                }
              },
              child: Text(
                photoURL!= null ? 'Change Profile Picture' : 'Upload Profile Picture',
              ),
            ),

获取并显示个人资料图片:

FutureBuilder<User?>(
              future: Provider.of<ApplicationState>(context).authStateChanges.first,
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  print(snapshot.error);
                  return const Text('Error loading user data');
                }
                if (snapshot.hasData) {
                  final user = snapshot.data!;
                  final photoURL = user.photoURL;
                  if (photoURL != null) {
                    return ClipRRect(
                      borderRadius: BorderRadius.circular(75),
                      child: Image.network(
                        photoURL,
                        width: 150,
                        height: 150,
                        fit: BoxFit.cover,
                      ),
                    );
                  } else {
                    return const Icon(Icons.person, size: 100);
                  }
                }
              },
            ),
firebase google-cloud-platform google-cloud-firestore firebase-authentication firebase-storage
1个回答
0
投票

我将每个用户的个人资料图像存储在 Firebase Storage 中。

这是一种常见的方法。 Cloud Storage for Firebase 是一款旨在存储文件的 Firebase 产品。

用户对象的

photoURL
属性指向用户从 Firebase Storage 上传的图像,然后将其显示在个人资料页面上。

是的,这就是 photoURL 属性的用途,用于保存 Firebase 存储位置中存在的图像的 URL 或由第三方 OAuth 提供商(例如 Google)提供的图像的 URL。

问题是,当我在两台设备上以同一用户身份登录并更新一台设备上的个人资料图片时,它不会在第二台设备上更新。我必须退出然后重新登录才能更新。

这是预期的行为,因为其他设备不知道其他设备上发生了什么,除非您注销然后再次登录以获取新数据。对于 User 对象内部发生的变化,它没有实时监听器。确实有一个 authStateChanges() 其中:

通知用户登录状态的更改(例如登录或注销)。

但是它不会通知

photoURL
属性的值是否已更改。

为什么不实时更新?

因为没有侦听器这样做。

我是否必须将 photoURL 存储在 Firestore 数据库中并从那里进行更新?

是的,这确实是一个解决方案。您可以在 Firestore 中的文档中添加

photoURL
属性的值并监听实时更新

© www.soinside.com 2019 - 2024. All rights reserved.