Replies: 4 comments 6 replies
-
Check out the docs: https://gorouter.dev/redirection |
Beta Was this translation helpful? Give feedback.
-
we're way out of my depth wrt how bloc works. @jopmiddelkamp do you have some advice here? |
Beta Was this translation helpful? Give feedback.
-
Asking in a different way, is there a way to get access to the GoRouter in context before
|
Beta Was this translation helpful? Give feedback.
-
Please don't copy-paste this as I've stripped a lot of error handling and modified it a little so it matches your example. But what I personally did was the following. FYI: You can execute your magic in the main.cs BlocOverrides.runZoned(
() async {
final authCubit = AuthCubit()..init();
// Make auth cubit initialize when the splash screen is active
await authCubit.onReady;
runApp(App(
appCubit: appCubit,
));
},
), auth_cubit.dart class AuthCubit extends CubitBase<AuthState> with ReadyMixin {
AuthCubit({}) : super(const AuthState.initializing());
Stream<bool> get routeRefreshStream => stream
.where((state) => state is AuthLoaded)
.map((state) => (state as AuthLoaded).hasSignedIn)
.distinct();
Future<void> init() async {
emit(AuthState.loaded( ... ));
readyCompleter.completeIfNotCompleted();
}
} ready_mixin.dart @optionalTypeArgs
mixin ReadyMixin<TResult> {
@protected
Completer<TResult> readyCompleter = Completer<TResult>();
bool get isReady => readyCompleter.isCompleted;
bool get isNotReady => !isReady;
Future<TResult> get onReady => readyCompleter.future;
} app.dart class App extends StatefulWidget {
const App({
required this.authCubit,
Key? key,
}) : super(key: key);
final AuthCubit authCubit;
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
late GoRouter router;
@override
void initState() {
super.initState();
router = GoRouter(
routes: kRoutes,
urlPathStrategy: UrlPathStrategy.path,
debugLogDiagnostics: kDebugMode,
redirect: (state) => topLevelGuard(
widget.authCubit,
state,
),
refreshListenable: GoRouterRefreshStream(
widget.authCubit.routeRefreshStream,
),
);
}
BlocProvider<AuthCubit> _blocProvider(
WidgetBuilder builder,
) {
return BlocProvider.value(
value: widget.authCubit,
child: Builder(builder: builder),
);
}
@override
Widget build(
BuildContext context,
) {
return _blocProvider((context) {
return MaterialApp.router(
title: '...',
// Routing
routerDelegate: router.routerDelegate,
routeInformationParser: router.routeInformationParser,
);
});
}
} top_level_guard.dart String? topLevelGuard(
AuthCubit authCubit,
GoRouterState state,
) {
final authState = authCubit.state;
if (authState is! AuthLoaded) {
return null;
}
// Do your magic here
return null;
} Please let me know if this helped you. |
Beta Was this translation helpful? Give feedback.
-
I have a
BlocConsumer<AuthCubit, AuthState>
which listens toloaded: (s)
wheres.token
contains a new user token when the user logs in or out:I saw https://github.com/csells/go_router/blob/main/go_router/example/lib/router_stream_refresh.dart
but unsure how to adopt
to listen to bloc states after being emitted:
emit(AuthState.loaded(token: _tokenController.value));
Beta Was this translation helpful? Give feedback.
All reactions