集成Firebase Auth UI后GoRouter首次安装触发GoException问题
问题描述
添加Firebase Auth UI依赖后,用GoRouter实现页面导航的Flutter应用,首次安装启动时会抛出以下错误:
GoException: no routes for location:
com.app.myapp://google/link?request_ip_version=IP_V4&match_messa
ge=No%20pre-install%20link%20matched%20for%20this%20device.
刷新应用后可正常运行,但卸载重装并首次启动时,该错误会再次出现。需要注意的是,尚未实际使用Firebase Auth UI功能就触发了此问题。
相关代码(main.dart)
void main() async { // Ensure the widgets are initialized WidgetsFlutterBinding.ensureInitialized(); // Load firebase await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); // Initialize the shared preferences final prefs = await SharedPreferences.getInstance(); // Intialize the app settings final repository = AppSettingsImpl(prefs); // Run the main app runApp( ProviderScope( overrides: [ settingsRepositoryProvider.overrideWithValue(repository), ], child: const MyApp(), ), ); } class MyApp extends ConsumerWidget { // Constructor const MyApp({super.key}); // Create @override Widget build(BuildContext context, WidgetRef ref) { // Create the text theme TextTheme textTheme = createTextTheme(context, "Poppins", "Lato"); // Create the material theme MaterialTheme theme = MaterialTheme(textTheme); // Return the material app return MaterialApp.router( theme: theme.light(), darkTheme: theme.dark(), highContrastTheme: theme.lightHighContrast(), highContrastDarkTheme: theme.darkHighContrast(), themeMode: ref.watch( glamSettingsNotifierProvider.select( (s) => s.theme, ), ), routerConfig: router, ); } } final router = GoRouter( initialLocation: GlamDestinations.login.route.path, routes: [ ShellRoute( builder: (context, state, child) { return ScaffoldWithNavBar(child: child); }, routes: [ GoRoute( path: GlamDestinations.login.route.path, builder: (context, state) => const LoginViewExperimental(), ), GoRoute( path: GlamDestinations.home.route.path, builder: (context, state) => const HomeView(), ), GoRoute( path: GlamDestinations.explore.route.path, builder: (context, state) => const ExploreView(), ), GoRoute( path: GlamDestinations.bookings.route.path, builder: (context, state) => const BookingsView(), ), GoRoute( path: GlamDestinations.profile.route.path, builder: (context, state) => const ProfileView(), ), ], ), ], );
解决方案
这个问题是Firebase Auth UI自动生成的动态链接在应用首次启动时触发了GoRouter的路由匹配,但你的路由表中没有对应的/google/link路径导致的。以下是几种可行的解决方法:
- 添加通配符路由捕获未匹配路径:在GoRouter的路由列表末尾添加一个通配符路由,把所有未匹配的路径自动跳转到初始页面,避免抛出错误。修改后的router代码如下:
final router = GoRouter( initialLocation: GlamDestinations.login.route.path, routes: [ // 原有的ShellRoute和子路由保持不变 ShellRoute( builder: (context, state, child) { return ScaffoldWithNavBar(child: child); }, routes: [ GoRoute( path: GlamDestinations.login.route.path, builder: (context, state) => const LoginViewExperimental(), ), GoRoute( path: GlamDestinations.home.route.path, builder: (context, state) => const HomeView(), ), GoRoute( path: GlamDestinations.explore.route.path, builder: (context, state) => const ExploreView(), ), GoRoute( path: GlamDestinations.bookings.route.path, builder: (context, state) => const BookingsView(), ), GoRoute( path: GlamDestinations.profile.route.path, builder: (context, state) => const ProfileView(), ), ], ), // 新增通配符路由 GoRoute( path: '*', builder: (context, state) => const LoginViewExperimental(), ), ], );
配置Firebase忽略该动态链接:在Firebase控制台的动态链接设置中,将
com.app.myapp://google/link添加到忽略列表,阻止应用启动时自动触发该链接。延迟初始化Firebase Auth UI:如果暂时不需要使用Firebase Auth UI功能,可以延迟初始化相关模块,等应用完成启动流程后再初始化,避免其提前触发动态链接逻辑。
内容的提问来源于stack exchange,提问作者Mervin Hemaraju
相关产品推荐
相关产品推荐

