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

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

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 &#39;package:firebase_auth/firebase_auth.dart&#39; hide EmailAuthProvider;
import &#39;package:firebase_core/firebase_core.dart&#39;;
import &#39;package:firebase_ui_auth/firebase_ui_auth.dart&#39;;
import &#39;package:flutter/material.dart&#39;;

import &#39;firebase_options.dart&#39;;

Future&lt;void&gt; 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&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: RegisterScreen(
        providers: [EmailAuthProvider()],
        actions: [
          AuthStateChangeAction&lt;SignedIn&gt;((context, state) {
            print(&#39;AuthStateChangeAction()&#39;);
          }),
        ],
      ),
      floatingActionButton: IconButton(
        icon: const Icon(Icons.close),
        onPressed: () async {
          print(&#39;FirebaseAuth.instance.signOut()&#39;);
          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(&#39;FirebaseAuth.instance.authStateChanges()&#39;);
  });
}

答案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&lt;SignedIn&gt;((context, state) {
        print(&#39;AuthStateSignedIn&#39;);
      }),
      AuthStateChangeAction&lt;UserCreated&gt;((context, state) {
        print(&#39;AuthStateUserCreated&#39;);
      }),
    ],

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:

确定