gpt4 book ai didi

Flutter 自动路由 : How to set initial route programmatically?

转载 作者:行者123 更新时间:2023-12-05 08:10:54 29 4
gpt4 key购买 nike

我有一个带主屏幕的登录屏幕。用户登录后,它将用户重定向到主屏幕。但是,我有一个 bloc 检查用户之前是否经过身份验证,如果是,它会将用户直接重定向到主屏幕。

但是,它并没有按预期工作。 “isLoggedIn” bool 值发生变化,但 UI 没有反应。如何解决?

这是代码

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:harnasik/core/router/app_router.gr.dart';
import 'package:harnasik/core/style/custom_colors.dart';
import 'package:harnasik/features/authorization/presentation/blocs/log_in/log_in_bloc.dart';
import 'package:harnasik/generated/l10n.dart';
import 'package:harnasik/injection_container.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await InjectionContainer().init();
runApp(const HarnasikApp());
}

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

@override
_HarnasikAppState createState() => _HarnasikAppState();
}

class _HarnasikAppState extends State<HarnasikApp> {
late final LogInBloc logInBloc;
late bool isLoggedIn;
final AppRouter _appRouter = sl();

@override
void initState() {
logInBloc = sl();
super.initState();
isLoggedIn = false;
logInBloc..add(CheckIfUserLoggedInEvent());
}

@override
void dispose() {
logInBloc.close();
super.dispose();
}

@override
Widget build(BuildContext context) {
return BlocListener(
bloc: logInBloc,
listener: (context, state) {
if (state is LoggedInState) {
isLoggedIn = true;
} else if (state is LogInErrorState) {
print('Error'); // For tests only
}
},
child: MaterialApp.router(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: CustomColors.blue,
fontFamily: 'Lato',
),
routerDelegate: _appRouter.delegate(
initialRoutes: [
if (!isLoggedIn) const LogInScreen() else const HomeScreen(),
],
),
routeInformationParser: _appRouter.defaultRouteParser(),
localizationsDelegates: const [
S.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate
],
supportedLocales: S.delegate.supportedLocales,
),
);
}
}

最佳答案

我使用这样的 auto_route 守卫解决了同样的问题:

/// An auto_route guard object that routes the user to our landing page if
/// signed in and to the home page if not signed in
class GetInitialRoute extends AutoRouteGuard {

@override
void onNavigation(NavigationResolver resolver, StackRouter router) {

// Here we check if the user is logged in using FirebaseAuth (not currentUser -->
// not logged in)
if (FirebaseAuth.instance.currentUser == null) {
// if we do not have a user yet, we resume navigation to the sign in page
// with path: '/'
resolver.next(true);
} else {
// if we already have a logged in user, we push them to our home page
router.push(const HomeRoute());
}
}
}

请阅读这篇关于 auto_route 中身份验证的文章 LINK和这篇关于 route_guards 的文章 LINK了解更多。这些文章还解释了如何将守卫应用到您的路线。

关于Flutter 自动路由 : How to set initial route programmatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68960334/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com