![](/img/trans.png)
[英]How to pass a bloc to another screen using go_router on flutter
[英]how to use flutter_bloc with go_router
我構建了一個使用 flutter_bloc 的應用程序。 我想使用 go_router 進行導航。但是對於動態路由,我如何使用帶有 flutter_bloc 的 GoRouter refreshListener 參數
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:
);
由於 bloc 還以 stream 的形式返回狀態,因此您可以直接使用它。 你可以這樣做 ->
refreshListenable = GoRouterRefreshStream(authenticationBloc.stream),
也許你可以做這樣的事情:
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();
}
}
代碼來自這個答案,希望你明白。
對我來說,將 changeNotifier 與 Bloc class 混合並從事件中調用 notififyListener()
這是我的集團 class
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());
}
}```
This is 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.