在我的flutter应用程序中,我正在使用go_router和flutter_riverpod构建路由,但当我使用提供商设置路由时,我开始出现此错误
The following assertion was thrown building
DefaultSelectionStyle:
unknown route name: /auth/:uid
'package:go_router/src/configuration.dart':
Failed assertion: line 243 pos 12:
'_nameToPath.containsKey(name)'
字符串
这是我的主.dart文件,我在这里调用routes。
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) async {
await Firebase.initializeApp();
await FirebaseAppCheck.instance
.activate(androidProvider: AndroidProvider.playIntegrity);
runApp(
const ProviderScope(
child: MyApp(),
),
);
});
FlutterError.demangleStackTrace = (StackTrace stack) {
if (stack is stack_trace.Trace) return stack.vmTrace;
if (stack is stack_trace.Chain) return stack.toTrace().vmTrace;
return stack;
};
}
class MyApp extends ConsumerWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context, WidgetRef ref) {
return MaterialApp.router(
debugShowCheckedModeBanner: false,
title: 'DiscountLo',
theme: AppTheme.theme,
locale: ref.watch(languageNotifierProvider),
localizationsDelegates: {
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate
},
supportedLocales: [Locale("en"), Locale("hi")],
routerDelegate: ref.watch(routeProvider).routerDelegate,
routeInformationParser: ref.watch(routeProvider).routeInformationParser,
routeInformationProvider: ref.watch(routeProvider).routeInformationProvider,
);
}
}
型
我的router.dart文件,在那里我创建了一个提供程序,我试图调用重定向来处理状态管理和保护路由。
final routeProvider = Provider<GoRouter>(
(ref) {
return GoRouter(
routes: [
GoRoute(
name: "home",
path: RouteNameConstants.home,
pageBuilder: (BuildContext context, GoRouterState state) =>
MaterialPage(
child: const HomeView(),
),
),
GoRoute(
name: "splash",
path: RouteNameConstants.splash,
pageBuilder: (BuildContext context, GoRouterState state) =>
MaterialPage(
child: const SplashScreen(),
),
),
GoRoute(
name: "onboard",
path: RouteNameConstants.onboarding,
pageBuilder: (BuildContext context, GoRouterState state) =>
MaterialPage(
child: const OnboardingView(),
),
),
GoRoute(
name: "error",
path: RouteNameConstants.error,
pageBuilder: (BuildContext context, GoRouterState state) =>
MaterialPage(
child: ErrorPage(text: state.pathParameters['text']!),
),
),
GoRoute(
name: "auth",
path: RouteNameConstants.authState,
pageBuilder: (BuildContext context, GoRouterState state) {
return MaterialPage(
child: AuthChanges(uid: state.pathParameters['uid']!),
);
},
),
GoRoute(
name: "filter",
path: RouteNameConstants.filter,
pageBuilder: (BuildContext context, GoRouterState state) {
return MaterialPage(
child: FilterView(
index: double.tryParse(state.pathParameters['index']!)!.toInt(),
category: state.pathParameters['category']!,
),
);
},
),
],
redirect: (context, state) {
final loginState = ref.watch(authStateChangeProvider);
if (loginState.isLoading) {
return RouteNameConstants.splash;
} else if (loginState.hasError) {
return state.namedLocation(
RouteNameConstants.error,
pathParameters: {
"text": loginState.error.toString(),
},
);
} else if (loginState.value != null) {
return state.namedLocation(RouteNameConstants.authState,
pathParameters: {"uid": loginState.value!.uid});
} else {
return RouteNameConstants.onboarding;
}
},
);
},
);
型
我的路由器路径名常量
class RouteNameConstants {
static const String home = '/';
static const String splash = '/splash';
static const String onboarding = '/onboarding';
static const String authState = '/auth/:uid';
static const String error = '/error/:text';
static const String filter = '/filter';
}
型
请如果有人可以帮助我
1条答案
按热度按时间8ehkhllq1#
我在吸毒
字符串
作为路径在GoRoute而不是现在我使用作为名称和定义的路径这种方式解决了我的问题
型