英文:
Flutter firebase_ui_auth RegisterScreen() does not trigger AuthStateChangeAction event
问题
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事件通常会触发,可以使用,但在登录情况下,AuthStateChangeAction以期望的方式触发。
这可能作为一种解决方法:
@override
void initState() {
super.initState();
FirebaseAuth.instance.authStateChanges().listen((user) {
print('FirebaseAuth.instance.authStateChanges()');
});
}
英文:
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()');
});
}
答案1
得分: 1
有两个单独的处理程序分别处理'SignedIn'和'UserCreated'事件。
第二个处理程序改变了身份验证状态,但不触发'SignedIn'事件。
看起来像这样工作:
actions: [
AuthStateChangeAction<SignedIn>((context, state) {
print('AuthStateSignedIn');
}),
AuthStateChangeAction<UserCreated>((context, state) {
print('AuthStateUserCreated');
}),
],
英文:
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');
}),
],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论