搜索插件、代码仓与 APP 分类

插件详情v1.1.0

firebase_user_stream

该包旨在提供 firebase_auth 中缺失的功能,即能够从服务器重新加载用户数据,并获取包含新数据的流更新。

近 30 天下载
41
喜欢数
20
Pub 分数
40

安装插件

flutter pub add firebase_user_stream

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

firebase_user_stream 包通过从服务器实时重新加载用户数据并提供更新的用户信息流,扩展了 Firebase Auth。它填补了 firebase_auth 缺乏内置支持刷新用户数据和提供实时更新的空白。这对于在应用会话中保持准确的用户状态特别有用,尤其是在服务器上的用户数据发生变化时。该包与现有的 Firebase Auth 工作流程无缝集成,并确保 UI 组件反映最新的用户详情,而无需手动刷新逻辑。

适合场景

  • 实时用户数据同步
  • 在服务器端更新后刷新用户数据
  • 在UI中保持用户状态的最新
  • 实时处理用户资料更改
  • 避免认证流程中的过期用户数据

主要特点

  • 基于流的用户更新
  • 服务器端用户数据重新加载
  • 与 firebase_auth 的无缝集成
  • 认证状态更改后自动同步
  • 轻量且专注的功能

适合对象

  • 需要实时用户数据的应用程序
  • 需要实时更新的 Firebase Auth 用户
  • 开发人员避免手动用户刷新逻辑
  • 具有动态用户资料的项目
  • 使用 Firebase 的跨平台应用

使用建议

  • 需要设置 firebase_auth
  • 仅限用户数据刷新
  • 无额外的身份验证功能
  • 取决于 Firebase 后端的稳定性
  • 如果用户数据在登录后不发生变化,则不需要

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Firebase User Stream

pub package

A Package for subscribing to User reload updates.

ATTENTION

Since firebase_auth version 0.18.0 this library is mostly pointless as firebase_auth (finally) adds authStateChanges() which mostly substitutes this package, fixing the problem described below.

I mean mostly because here we can have further functionality like:

  • Reloading based on a predicate
  • Reload and get the user in the same line.

Although that's not useful to most people.

The (old, pre firebase_auth version 0.18.0) problem

The FirebaseAuth Flutter plugin provides a Stream<FirebaseUser> with onAuthStateChanged, which is useful for getting updates when a user signs-in or signs-out, but it fails to provide an update when the user data itself changes.

A common problem is when we want to detect if the user has just verified his e-mail address, in other words get if FirebaseUser.isEmailVerified changed its value, as this is updated server-side and not pushed to the app.

FirebaseAuth.currentUser() will only detect changes made locally to the user, but if any server-side changes occur, it won't detect them, unless FirebaseUser.reload() is called first, then we need to call FirebaseAuth.currentUser() again to get the reloaded user, but still this user won't be emitted by onAuthStateChanged.

The solution

There are two ways we can get a new FirebaseUser, with the latest information on the server:

  1. Do it manually:
Future<FirebaseUser> reloadCurrentUser() async {
  FirebaseUser oldUser = await FirebaseAuth.instance.currentUser();    
  oldUser.reload();
  FirebaseUser newUser = await FirebaseAuth.instance.currentUser();
  // Add newUser to a Stream, maybe merge this Stream with onAuthStateChanged?
  return newUser; 
}
  1. Let us do it for you!

Getting Started

After you install this package you can use FirebaseUserReloader to get reload updates using the onUserReloaded and onAuthStateChangedOrReloaded streams.

FirebaseUserReloader internally uses the FirebaseAuth instance returned by FirebaseAuth.instance, so your entire App will have the same Singleton instance, you don't have to configure anything, it just works out of the box.

In order to get reload updates just do:

var subscription = FirebaseUserReloader.onUserReloaded.listen((user) {
  // A new user will be printed each time there's a reload
  print(user);
});

// This will trigger a reload and the reloaded user will be emitted by onUserReloaded
FirebaseUserReloader.reloadCurrentUser();

subscription.cancel();

Each time you call reloadCurrentUser() a reloaded user will be emitted by onUserReloaded.

Keep in mind that onUserReloaded will emit reloaded users, they might be new, or just the current one if the user data didn't change.

As a bonus, you can provide a predicate to reloadCurrentUser(), in order to only get reloaded users that matter to you:

var subscription = FirebaseUserReloader.onUserReloaded.listen((user) {
  // A new user will be printed each time there's a reload
  print(user);
});

// This will trigger a reload and the reloaded user will be emitted by onUserReloaded
// only if isEmailVerified == true
FirebaseUserReloader.reloadCurrentUser((user) => user.isEmailVerified);

subscription.cancel();

You also can get the reloaded user as the return value of reloadCurrentUser, but in this case, the predicate will be ignored, and the reloaded user will always be returned.

var user = await FirebaseUserReloader.reloadCurrentUser();

If you want to listen for updates on sign-ins, sign-outs and user reloads, use onAuthStateChangedOrReloaded instead, it's just a convenient merge of onUserReloaded and FirebaseAuth.onAuthStateChanged and it works as a broadcast Behavior Subject:

var subscription = FirebaseUserReloader.onAuthStateChangedOrReloaded.listen((user) {
  // A new user will be printed each time there's a reload, login or logout
  print(user);
});

// This will trigger a reload and the reloaded user will be emitted by onUserReloaded
// only if isEmailVerified == true
FirebaseUserReloader.reloadCurrentUser((user) => user.isEmailVerified);

subscription.cancel();

Both onAuthStateChangedOrReloaded and onUserReloaded are broadcast streams.

Testing

This library uses static methods for easiness of usage, but this doesn't limit its testability.

FirebaseUserReloader can be injected with a mocked instance of FirebaseAuth for unit testing.

For any examples on how to control its behavior under tests, please take a look at our own tests inside the test folder.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。