Flutter go_router抛出错误未知路由名称:/auth/:uid,带有“_nameToPath.containsKey(name)”

zynd9foi  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(86)

在我的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';
}


请如果有人可以帮助我

8ehkhllq

8ehkhllq1#

我在吸毒

class RouteNameConstants {
  static const String home = 'home';
  static const String splash = 'splash';
  static const String onboarding = 'onboard';
  static const String authState = 'auth';
  static const String error = 'error';
  static const String filter = 'filter';
  static const String profile = 'profile';
  static const String phone = 'phone';
  static const String otp = 'otp';
  static const String profilecontent = 'profilecontent';
}

字符串
作为路径在GoRoute而不是现在我使用作为名称和定义的路径这种方式解决了我的问题

import 'package:discountlo/core/route_constant.dart';
import 'package:discountlo/features/auth/views/otp_view.dart';
import 'package:discountlo/features/auth/views/phone_view.dart';
import 'package:discountlo/features/home/view/filter_view.dart';
import 'package:discountlo/features/home/view/profile_content_view.dart';
import 'package:discountlo/features/home/view/profile_view.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:discountlo/features/auth/views/onboarding_screen.dart';
import 'package:discountlo/features/auth/views/splash_view.dart';
import 'package:discountlo/features/home/view/home_view.dart';
import 'auth_changes.dart';
import 'error_page.dart';
import 'package:flutter/material.dart';
import 'features/auth/controller/auth_controller.dart';

final _key = GlobalKey<NavigatorState>();

final routeProvider = Provider<GoRouter>(
  (ref) {
    return GoRouter(
      navigatorKey: _key,
      debugLogDiagnostics: true,
      routes: [
        GoRoute(
          name: RouteNameConstants.home,
          path: "/",
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: const HomeView(),
          ),
        ),
        GoRoute(
          path: "/splash",
          name: RouteNameConstants.splash,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: const SplashScreen(),
          ),
        ),
        GoRoute(
          path: "/otp",
          name: RouteNameConstants.otp,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: OTPView(
              phoneNumber: state.pathParameters['phoneNumber']!,
            ),
          ),
        ),
        GoRoute(
          path: "/phone",
          name: RouteNameConstants.phone,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: const PhoneView(),
          ),
        ),
        GoRoute(
          path: "/profile",
          name: RouteNameConstants.profile,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: const ProfileView(),
          ),
        ),
        GoRoute(
          path: "/profilecontent",
          name: RouteNameConstants.profilecontent,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: ProfileContentView(
              clicked: state.pathParameters['clicked']!,
            ),
          ),
        ),
        GoRoute(
          path: "/onboard",
          name: RouteNameConstants.onboarding,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: const OnboardingView(),
          ),
        ),
        GoRoute(
          path: "/error/:text",
          name: RouteNameConstants.error,
          pageBuilder: (BuildContext context, GoRouterState state) =>
              MaterialPage(
            child: ErrorPage(text: state.pathParameters['text']!),
          ),
        ),
        GoRoute(
          path: "/auth/:uid",
          name: RouteNameConstants.authState,
          pageBuilder: (BuildContext context, GoRouterState state) {
            return MaterialPage(
              child: AuthChanges(uid: state.pathParameters['uid']!),
            );
          },
        ),
        GoRoute(
          path: "/filter/:index/:category",
          name: RouteNameConstants.filter,
          pageBuilder: (BuildContext context, GoRouterState state) {
            debugPrint("sdvf ${state.pathParameters}");
            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 state.namedLocation(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 state.namedLocation(RouteNameConstants.onboarding);
        }
      },
    );
  },
);

相关问题