【问题标题】:how to use flutter_bloc with go_router如何在 gorouter 中使用颤振块
【发布时间】:2022-01-16 05:15:37
【问题描述】:

我已经构建了一个使用 flutter_bloc 的应用程序。我想使用 go_router 进行导航。但是对于动态路由,我如何将 GoRouter refreshListener 参数与 flutter_bloc 一起使用

GoRouter(
  routes: [
    GoRoute(
      path: '/',
      name: 'home',
      pageBuilder: (context, state) => HomePage.page(),
    ),
    GoRoute(
      path: '/login',
      name: 'login',
      pageBuilder: (context, state) => LoginPage.page(),
    ),
  ],
  redirect: (state) {
    final isLoggedIn =
        bloc.state.status == AuthenticationStatus.authenticated;
    final isLoggingIn = state.location == '/login';

    if (!isLoggingIn && !isLoggingIn) return '/login';
    if (isLoggedIn && isLoggingIn) return "/";

    return null;
  },
  refreshListenable: 
   );

【问题讨论】:

    标签: flutter flutter-bloc flutter-navigation


    【解决方案1】:

    也许你可以这样做:

    class AuthStateNotifier extends ChangeNotifier {
      late final StreamSubscription<AuthState> _blocStream;
      AuthStateProvider(AuthBloc bloc) {
        _blocStream = bloc.stream.listen((event) {
          notifyListeners();
        });
      }
    
      @override
      void dispose() {
        _blocStream.cancel();
        super.dispose();
      }
    }
    

    代码来自this answer,希望你明白。

    【讨论】:

    • 感谢您的回答。但我发现更容易将 changenotifier 与 bloc 类和 notifylistener() 混合用于特定事件,在这种情况下更改 authstate
    • minxin 解决方案听起来更容易:)
    【解决方案2】:

    对我来说,将 changeNotifier 与 Bloc 类混合并从事件中调用 notifyListener() 是可行的

    这是我的集体课

    class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState>
        with ChangeNotifier {
      AuthenticationBloc(
          {required AuthenticationRepository authenticationRepository})
          : _authenticationRepository = authenticationRepository,
            super(const AuthenticationState.unknown()) {
        on<AppStarted>(_onAppStarted);
    
        on<AuthenticationUserChanged>(_onAuthenticationUserChanged);
    
        on<AuthenticationLogoutRequested>(_onAuthenticationLogoutRequested);
    
        _userSubscription = _authenticationRepository.user
            .listen((user) => add(AuthenticationUserChanged(user)));
      }
    
      final AuthenticationRepository _authenticationRepository;
    
      late StreamSubscription<User> _userSubscription;
    
    
      @override
      Future<void> close() {
        _userSubscription.cancel();
        return super.close();
      }
    
    
      FutureOr<void> _onAppStarted(
          AppStarted event, Emitter<AuthenticationState> emit) {
        emit(AuthenticationState.unknown());
      }
    
      FutureOr<void> _onAuthenticationUserChanged(
          AuthenticationUserChanged event, Emitter<AuthenticationState> emit) {
        final status = event.user != User.empty
            ? AuthenticationState.authenticated(event.user)
            : const AuthenticationState.unauthenticated();
        emit(status);
        notifyListeners();
      }
    
      FutureOr<void> _onAuthenticationLogoutRequested(
          AuthenticationLogoutRequested event, Emitter<AuthenticationState> emit) {
        unawaited(_authenticationRepository.logOut());
      }
    }
    

    这是GoRouter

    GoRouter routes(AuthenticationBloc bloc) {
      return GoRouter(
          routes: [
            GoRoute(
              path: '/',
              name: 'home',
              builder: (context, state) => const HomePage(),
            ),
            GoRoute(
              path: '/login',
              name: 'login',
              builder: (context, state) => const LoginPage(),
            ),
          ],
          redirect: (state) {
            final isLoggedIn =
                bloc.state.status == AuthenticationStatus.authenticated;
            final isLoggingIn = state.location == '/login';
            print(isLoggedIn);
    
            if (!isLoggedIn && !isLoggingIn) return '/login';
            if (isLoggedIn && isLoggingIn) return '/';
    
            return null;
          },
          refreshListenable: bloc);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-29
      • 2020-11-22
      • 2019-01-23
      • 2021-03-29
      • 2020-08-25
      • 2020-05-03
      • 2018-06-16
      • 2022-11-12
      相关资源
      最近更新 更多