Flutter firebase_ui_auth的RegisterScreen()未触发AuthStateChangeAction事件。

huangapple go评论107阅读模式
英文:

Flutter firebase_ui_auth RegisterScreen() does not trigger AuthStateChangeAction event

问题

AuthStateChangeAction事件在注册期间从不触发 - 无论是使用RegisterScreen()还是SignInScreen()

  1. import 'package:firebase_auth/firebase_auth.dart' hide EmailAuthProvider;
  2. import 'package:firebase_core/firebase_core.dart';
  3. import 'package:firebase_ui_auth/firebase_ui_auth.dart';
  4. import 'package:flutter/material.dart';
  5. import 'firebase_options.dart';
  6. Future<void> main() async {
  7. WidgetsFlutterBinding.ensureInitialized();
  8. await Firebase.initializeApp(
  9. options: DefaultFirebaseOptions.currentPlatform,
  10. );
  11. runApp(const MyApp());
  12. }
  13. class MyApp extends StatelessWidget {
  14. const MyApp({super.key});
  15. @override
  16. Widget build(BuildContext context) {
  17. return MaterialApp(
  18. home: const MyHomePage(),
  19. );
  20. }
  21. }
  22. class MyHomePage extends StatefulWidget {
  23. const MyHomePage({super.key});
  24. @override
  25. State<MyHomePage> createState() => _MyHomePageState();
  26. }
  27. class _MyHomePageState extends State<MyHomePage> {
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(),
  32. body: RegisterScreen(
  33. providers: [EmailAuthProvider()],
  34. actions: [
  35. AuthStateChangeAction<SignedIn>((context, state) {
  36. print('AuthStateChangeAction()');
  37. }),
  38. ],
  39. ),
  40. floatingActionButton: IconButton(
  41. icon: const Icon(Icons.close),
  42. onPressed: () async {
  43. print('FirebaseAuth.instance.signOut()');
  44. await FirebaseAuth.instance.signOut();
  45. },
  46. ),
  47. );
  48. }
  49. }

原生FirebaseAuth事件通常会触发,可以使用,但在登录情况下,AuthStateChangeAction以期望的方式触发。

这可能作为一种解决方法:

  1. @override
  2. void initState() {
  3. super.initState();
  4. FirebaseAuth.instance.authStateChanges().listen((user) {
  5. print('FirebaseAuth.instance.authStateChanges()');
  6. });
  7. }
英文:

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

  1. import &#39;package:firebase_auth/firebase_auth.dart&#39; hide EmailAuthProvider;
  2. import &#39;package:firebase_core/firebase_core.dart&#39;;
  3. import &#39;package:firebase_ui_auth/firebase_ui_auth.dart&#39;;
  4. import &#39;package:flutter/material.dart&#39;;
  5. import &#39;firebase_options.dart&#39;;
  6. Future&lt;void&gt; main() async {
  7. WidgetsFlutterBinding.ensureInitialized();
  8. await Firebase.initializeApp(
  9. options: DefaultFirebaseOptions.currentPlatform,
  10. );
  11. runApp(const MyApp());
  12. }
  13. class MyApp extends StatelessWidget {
  14. const MyApp({super.key});
  15. @override
  16. Widget build(BuildContext context) {
  17. return MaterialApp(
  18. home: const MyHomePage(),
  19. );
  20. }
  21. }
  22. class MyHomePage extends StatefulWidget {
  23. const MyHomePage({super.key});
  24. @override
  25. State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
  26. }
  27. class _MyHomePageState extends State&lt;MyHomePage&gt; {
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(),
  32. body: RegisterScreen(
  33. providers: [EmailAuthProvider()],
  34. actions: [
  35. AuthStateChangeAction&lt;SignedIn&gt;((context, state) {
  36. print(&#39;AuthStateChangeAction()&#39;);
  37. }),
  38. ],
  39. ),
  40. floatingActionButton: IconButton(
  41. icon: const Icon(Icons.close),
  42. onPressed: () async {
  43. print(&#39;FirebaseAuth.instance.signOut()&#39;);
  44. await FirebaseAuth.instance.signOut();
  45. },
  46. ),
  47. );
  48. }
  49. }

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:

  1. @override
  2. void initState() {
  3. super.initState();
  4. FirebaseAuth.instance.authStateChanges().listen((user) {
  5. print(&#39;FirebaseAuth.instance.authStateChanges()&#39;);
  6. });
  7. }

答案1

得分: 1

有两个单独的处理程序分别处理'SignedIn'和'UserCreated'事件。
第二个处理程序改变了身份验证状态,但不触发'SignedIn'事件。
看起来像这样工作:

  1. actions: [
  2. AuthStateChangeAction<SignedIn>((context, state) {
  3. print('AuthStateSignedIn');
  4. }),
  5. AuthStateChangeAction<UserCreated>((context, state) {
  6. print('AuthStateUserCreated');
  7. }),
  8. ],
英文:

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:

  1. actions: [
  2. AuthStateChangeAction&lt;SignedIn&gt;((context, state) {
  3. print(&#39;AuthStateSignedIn&#39;);
  4. }),
  5. AuthStateChangeAction&lt;UserCreated&gt;((context, state) {
  6. print(&#39;AuthStateUserCreated&#39;);
  7. }),
  8. ],

huangapple
  • 本文由 发表于 2023年1月9日 18:40:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定