Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter firebase_ui_auth RegisterScreen() does not trigger AuthStateChangeAction event

AuthStateChangeAction event is never triggered during registration - both in case of using RegisterScreen() and SignInScreen()

import 'package:firebase_auth/firebase_auth.dart' hide EmailAuthProvider;
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:flutter/material.dart';

import 'firebase_options.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: RegisterScreen(
        providers: [EmailAuthProvider()],
        actions: [
          AuthStateChangeAction<SignedIn>((context, state) {
            print('AuthStateChangeAction()');
          }),
        ],
      ),
      floatingActionButton: IconButton(
        icon: const Icon(Icons.close),
        onPressed: () async {
          print('FirebaseAuth.instance.signOut()');
          await FirebaseAuth.instance.signOut();
        },
      ),
    );
  }
}

Native FirebaseAuth events are fired normally and they could be used but in case of Signing-in the AuthStateChangeAction is fired in desired way.

This might work as work-around:

@override
void initState() {
  super.initState();
  FirebaseAuth.instance.authStateChanges().listen((user) {
    print('FirebaseAuth.instance.authStateChanges()');
  });
}
like image 866
Tymoteusz Chmielewski Avatar asked Sep 13 '26 19:09

Tymoteusz Chmielewski


1 Answers

There is separate handler for 'SignedIn' and 'UserCreated' events. The second is changing auth state but does not fire SignedIn event. It looks like this works:

    actions: [
      AuthStateChangeAction<SignedIn>((context, state) {
        print('AuthStateSignedIn');
      }),
      AuthStateChangeAction<UserCreated>((context, state) {
        print('AuthStateUserCreated');
      }),
    ],
like image 52
Tymoteusz Chmielewski Avatar answered Sep 15 '26 09:09

Tymoteusz Chmielewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!