onAuthStateChanged property

Stream<FirebaseUser> onAuthStateChanged

Receive FirebaseUser each time the user signIn or signOut

Implementation

Stream<FirebaseUser> get onAuthStateChanged {
  Future<int> _handle;

  StreamController<FirebaseUser> controller;
  controller = StreamController<FirebaseUser>.broadcast(onListen: () {
    // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
    // https://github.com/flutter/flutter/issues/26431
    // ignore: strong_mode_implicit_dynamic_method
    _handle = channel.invokeMethod('startListeningAuthState',
        <String, String>{"app": app.name}).then<int>((dynamic v) => v);
    _handle.then((int handle) {
      _authStateChangedControllers[handle] = controller;
    });
  }, onCancel: () {
    _handle.then((int handle) async {
      // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
      // https://github.com/flutter/flutter/issues/26431
      // ignore: strong_mode_implicit_dynamic_method
      await channel.invokeMethod("stopListeningAuthState",
          <String, dynamic>{"id": handle, "app": app.name});
      _authStateChangedControllers.remove(handle);
    });
  });

  return controller.stream;
}