firebase_user_stream 0.1.0+4

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

FirebaseUser Stream #

pub package

A Package for subscribing to FirebaseUser reload updates.

The 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 changes are made server-side, it won't detect, unless FirebaseUser.reload() is called first and 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 onUserReloaded or onAuthStateChangedOrReloaded, it's pretty simple. 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:

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();

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, which can then be used for unit testing.

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

0.1.0+4 #

  • Added a pub badge to the README.

0.1.0+3 #

  • Added a Testability section to the README.

0.1.0+2 #

  • Made the description shorter.

0.1.0+1 #

  • Added an example.
  • Updated the package description.

0.1.0 #

  • Initial release.

example/example.dart

import 'package:firebase_user_stream/firebase_user_stream.dart';

/// This example is just to show how can you use the library on your code. As
/// this library works very tightly with [FirebaseAuth] it's hard to make a
/// simple example, as we will need to add a sign-in or sign-out in the example
/// itself, which goes beyond the libraries' purposes.
void main() {
  FirebaseUserReloader.onUserReloaded.listen((user) {
    print('Reloaded: $user');
  });

  FirebaseUserReloader.onAuthStateChangedOrReloaded.listen((user) {
    print('Reloaded or auth state changed: $user');
  });

  FirebaseUserReloader.reloadCurrentUser();
  FirebaseUserReloader.reloadCurrentUser((user) => user.isEmailVerified);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  firebase_user_stream: ^0.1.0+4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:firebase_user_stream/firebase_user_stream.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
69
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
84
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
firebase_auth ^0.15.3+1 0.15.5+3
flutter 0.0.0
rxdart ^0.23.1 0.23.1 0.24.0-dev.1
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.11 1.14.12
firebase 7.2.1
firebase_auth_platform_interface 1.1.7
firebase_auth_web 0.1.2
firebase_core 0.4.4+3
firebase_core_platform_interface 1.0.4
firebase_core_web 0.1.1+2
flutter_web_plugins 0.0.0
http 0.12.0+4
http_parser 3.1.4
js 0.6.1+1
matcher 0.12.6
meta 1.1.8
path 1.6.4
pedantic 1.9.0
plugin_platform_interface 1.0.2
quiver 2.1.3
sky_engine 0.0.99
source_span 1.7.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito ^4.1.1