mirror of
https://github.com/lucavenir/go_router_riverpod.git
synced 2025-08-06 14:59:53 +08:00
32 lines
931 B
Dart
32 lines
931 B
Dart
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import 'router_notifier.dart';
|
|
import 'routes.dart';
|
|
|
|
part 'router.g.dart';
|
|
|
|
final _key = GlobalKey<NavigatorState>(debugLabel: 'routerKey');
|
|
|
|
/// This simple provider caches our GoRouter.
|
|
/// This provider will never rebuild by design.
|
|
@riverpod
|
|
GoRouter router(RouterRef ref) {
|
|
// This instruction keeps this notifier alive
|
|
// We don't really care of its value, and neither we want to rebuild on its changes.
|
|
final sub = ref.listen(routerNotifierProvider, (_, __) {});
|
|
ref.onDispose(sub.close);
|
|
|
|
final notifier = ref.read(routerNotifierProvider.notifier);
|
|
|
|
return GoRouter(
|
|
navigatorKey: _key,
|
|
refreshListenable: notifier,
|
|
debugLogDiagnostics: true,
|
|
initialLocation: SplashRoute.path,
|
|
routes: notifier.routes,
|
|
redirect: notifier.redirect,
|
|
);
|
|
}
|