搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。