在注册过程中永远不会触发AuthStateChangeAction事件-无论是使用RegisterScreen()还是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();
},
),
);
}
}
原生FirebaseAuth事件被正常触发,它们可以被使用,但在sign -in的情况下,AuthStateChangeAction以期望的方式被触发。
这可以作为变通:
@override
void initState() {
super.initState();
FirebaseAuth.instance.authStateChanges().listen((user) {
print('FirebaseAuth.instance.authStateChanges()');
});
}
对于'SignedIn'和'UserCreated'事件有单独的处理程序。第二种是更改认证状态,但不触发SignedIn事件。看起来是这样的:
actions: [
AuthStateChangeAction<SignedIn>((context, state) {
print('AuthStateSignedIn');
}),
AuthStateChangeAction<UserCreated>((context, state) {
print('AuthStateUserCreated');
}),
],