Flutter Bloc auth不会听(卡在Ongenerateroute)

发布于 2025-01-22 23:57:38 字数 2321 浏览 4 评论 0 原文

IAM当前在Bloc上学习,我遵循此代码 https://github.com/felangel/bloc/tree/master/master/examples/flutter_login 但是,当IAM运行时,但是我陷入了Splashscreen,无法

使用粘贴垃圾箱穿过仪表板 /登录页IAM,因为我收到了警告,看起来您的帖子主要是代码;请添加更多详细信息。

mymain

class MyApp extends StatelessWidget {
  final _navigatorKey = GlobalKey<NavigatorState>();
  NavigatorState get _navigator => _navigatorKey.currentState!;
  @override
  Widget build(BuildContext context) {
    final _authenticationRepository = AuthRepository();
    return RepositoryProvider.value(
      value: _authenticationRepository,
      child: BlocProvider(
          create: (_) => AuthBloc(authRepository: _authenticationRepository),
          child: MaterialApp(
            navigatorKey: _navigatorKey,
            builder: (context, child) {
              return BlocListener<AuthBloc, AuthState>(
                listener: (context, state) {
                  switch (state.status) {
                    case AuthStatus.authenticated:
                      _navigator.pushAndRemoveUntil<void>(
                        DashBoard.route(),
                        (route) => false,
                      );
                      break;
                    case AuthStatus.unauthenticated:
                      _navigator.pushAndRemoveUntil<void>(
                        LoginPage.route(),
                        (route) => false,
                      );
                      break;
                    default:
                      _navigator.pushAndRemoveUntil<void>(
                        LoginPage.route(),
                        (route) => false,
                      );
                      break;
                  }
                },
                child: child,
              );
            },
            onGenerateRoute: (_) => SplashScreen.route(),
          )),
    );
  }
}

myauthbloc

https://pastebin.com/BQnhDeFE

myauthstate

https://pastebin.com/fmfnMwCV

myauthevent,

https://pastebin.com/tcEeqQsr

通过在飞溅屏幕上添加initstate并调用自动启动

  @override
  void initState() {
    context.read<AuthBloc>().add(AuthInitial());
    super.initState();
  }

iam currently learning on bloc , i follow this code https://github.com/felangel/bloc/tree/master/examples/flutter_login
but while iam running ,but i got stuck at splashscreen and cant going through the dashboard / loginpage

iam using paste bin because i got warning It looks like your post is mostly code; please add some more details.

MyMain

class MyApp extends StatelessWidget {
  final _navigatorKey = GlobalKey<NavigatorState>();
  NavigatorState get _navigator => _navigatorKey.currentState!;
  @override
  Widget build(BuildContext context) {
    final _authenticationRepository = AuthRepository();
    return RepositoryProvider.value(
      value: _authenticationRepository,
      child: BlocProvider(
          create: (_) => AuthBloc(authRepository: _authenticationRepository),
          child: MaterialApp(
            navigatorKey: _navigatorKey,
            builder: (context, child) {
              return BlocListener<AuthBloc, AuthState>(
                listener: (context, state) {
                  switch (state.status) {
                    case AuthStatus.authenticated:
                      _navigator.pushAndRemoveUntil<void>(
                        DashBoard.route(),
                        (route) => false,
                      );
                      break;
                    case AuthStatus.unauthenticated:
                      _navigator.pushAndRemoveUntil<void>(
                        LoginPage.route(),
                        (route) => false,
                      );
                      break;
                    default:
                      _navigator.pushAndRemoveUntil<void>(
                        LoginPage.route(),
                        (route) => false,
                      );
                      break;
                  }
                },
                child: child,
              );
            },
            onGenerateRoute: (_) => SplashScreen.route(),
          )),
    );
  }
}

MyAuthBloc

https://pastebin.com/BQnhDeFE

MyAuthState

https://pastebin.com/fmfnMwCV

MyAuthEvent

https://pastebin.com/tcEeqQsr

By Adding initstate on Splash Screen and calling autinital that's work

  @override
  void initState() {
    context.read<AuthBloc>().add(AuthInitial());
    super.initState();
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2025-01-29 23:57:38

您可以在存在存储库上的订阅:

 _authenticationStatusSubscription = _authenticationRepository.status.listen(
      (status) => add(AuthenticationStatusChanged(status)),
    );

authenticationRepository 触发拳头 authenticationStatusChanged 事件,

因为您是从Bloc库开始的,所以我建议您用 blocobserver

void main() {
  BlocOverrides.runZoned(
    () => runApp(const App()),
    blocObserver: AppBlocObserver(),
  );
}

/// Custom [BlocObserver] that observes all bloc and cubit state changes.
class AppBlocObserver extends BlocObserver {
  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    if (bloc is Cubit) print(change);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }
}

如果事件在那里,它将简化学习过程和验证

You can notice in the example subscription on repository is present:

 _authenticationStatusSubscription = _authenticationRepository.status.listen(
      (status) => add(AuthenticationStatusChanged(status)),
    );

AuthenticationRepository triggers the fist AuthenticationStatusChanged event

Since you are starting with Bloc library i suggest you wrap your main with BlocObserver:

void main() {
  BlocOverrides.runZoned(
    () => runApp(const App()),
    blocObserver: AppBlocObserver(),
  );
}

/// Custom [BlocObserver] that observes all bloc and cubit state changes.
class AppBlocObserver extends BlocObserver {
  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    if (bloc is Cubit) print(change);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }
}

It will ease up the learning process and verification if events correct events are there

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文