简体   繁体   English

Flutter Cubit 与 BlocListener:如何根据 state 导航用户?

[英]Flutter Cubit With BlocListener: How can I Navigate the user according to state?

I have a basic auth cubit, and in the state, it has just one variable.我有一个基本的 auth 肘,在 state 中,它只有一个变量。

@freezed
class AuthState with _$AuthState {
  const AuthState._();

  const factory AuthState({
    required bool isUserSignedIn,
  }) = _AuthState;

  factory AuthState.initial() => const AuthState(isUserSignedIn: false);
}

In the cubit part, I can easily check is user Logged in or not via FirebaseAuth.instance.authStateChanges().listen((User? user)) method in the debug console.在 cubit 部分,我可以通过调试控制台中的FirebaseAuth.instance.authStateChanges().listen((User? user))方法轻松检查用户是否登录。

Now, the problem starts in the landing page section.现在,问题从登陆页面部分开始。 There are some things that create problems.有些事情会产生问题。

First things first, you can see the landing page codes below.首先,您可以在下面看到着陆页代码。 Here, I have initState for the initial (when App starts, it works first), and after that, I return BlocListener for the listen to the cubit's state.在这里,我有初始的 initState(当应用程序启动时,它首先工作),然后,我返回 BlocListener 用于监听 cubit 的 state。 When I click the login button and also the log out button, it works properly (I can see it in the debug console, I can log out but the same screen continues to stay, after I reload the app, then I can see I have already exited).当我单击登录按钮和注销按钮时,它可以正常工作(我可以在调试控制台中看到它,我可以注销但相同的屏幕继续存在,重新加载应用程序后,我可以看到我有已经退出)。

In the BlocListener section, I tried to print some texts, but they do not work, I mean I can not see them in the debug console.在 BlocListener 部分,我尝试打印一些文本,但它们不起作用,我的意思是我在调试控制台中看不到它们。

class LandingPage extends StatefulWidget {
  const LandingPage({Key? key}) : super(key: key);

  @override
  State<LandingPage> createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  @override
  void initState() {
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      final bool isUserSignedIn =
          context.read<AuthCubit>().state.isUserSignedIn;
      if (isUserSignedIn) {
        AutoRouter.of(context).replace(const HomeRoute());
      } else if (!isUserSignedIn) {
        AutoRouter.of(context).replace(const SignInRoute());
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<AuthCubit, AuthState>(
      listenWhen: (p, c) => p.isUserSignedIn != c.isUserSignedIn,
      listener: (context, state) {
        final bool isUserSignedIn =
            context.read<AuthCubit>().state.isUserSignedIn;

        if (isUserSignedIn) {
          print("blocblocbloc1");
          AutoRouter.of(context).replace(const HomeRoute());
        } else {
          print("blocblocbloc2");
          AutoRouter.of(context).root.popUntilRoot();
        }
      },
      child: const Scaffold(
        body: Center(
          child: CircularProgressIndicator(color: Colors.white),
        ),
      ),
    );
  }
}

Apart from that, I use autoroute for the rotation (Since I do not want to see a direct widget, I want to navigate users according to their login state).除此之外,我使用自动路由进行轮换(因为我不想看到直接的小部件,所以我想根据用户的登录状态导航)。 Firstly I thought maybe AutoRouter does not work, but the print statement also does not work.首先我想也许 AutoRouter 不起作用,但 print 语句也不起作用。

Here I also share my main.dart file.这里也分享一下我的main.dart文件。

class AppWidget extends StatelessWidget {
  const AppWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _appRouter = AppRouter();
    return BlocProvider(
      lazy: true,
      create: (context) => AuthCubit(),
      child: MaterialApp.router(
        routeInformationParser: _appRouter.defaultRouteParser(),
        routerDelegate: _appRouter.delegate(),
        debugShowCheckedModeBanner: false,
      ),
    );
  }
}

Lastly, my router page is here.最后,我的路由器页面在这里。

@MaterialAutoRouter(
  replaceInRouteName: 'Page,Route',
  routes: <AutoRoute>[
    AutoRoute(page: LandingPage, initial: true),
    AutoRoute(
      page: HomePage,
    ),
    AutoRoute(
      page: SignInPage,
    ),
  ],
)
class $AppRouter {}

solution: use flow_builder to navigate user according to state解决方案:使用flow_builder根据state导航用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM