felangel / flow_builder

Flutter Flows made easy! A Flutter package which simplifies navigation flows with a flexible, declarative API.
https://pub.dev/packages/flow_builder
MIT License
389 stars 63 forks source link

Generated Route Access With Flow Builder #101

Open mstfkhazaal opened 1 year ago

mstfkhazaal commented 1 year ago

I'm new to this package, I have nested routes with Authentication and many many blocs and i use Generated Route Access

Generated Route

class AppRouter {
  AppRouter();
  static final loginBloc = LoginCubit(
    authBloc: authBloc,
  );
 static final authBloc = AuthBloc();
  static const String home = '/';
  static const String login = 'login';
  static const String profile = '/profile';
Route<dynamic> onGenerateRoute(RouteSettings settings) {
    switch (settings.name) {
      /// Auth
      case home:
      case login:
        return MaterialPageRoute<dynamic>(
          builder: (_) => MultiBlocProvider(
            providers: [
              BlocProvider<AuthBloc>.value(
                value: authBloc,
              ),
              BlocProvider<LoginCubit>.value(
                value: loginBloc,
              ),
            ],
            child: const AuthFlow(),
          ),
        );

      /// Profile
      case profile:
        return MaterialPageRoute<dynamic>(
          builder: (_) => MultiBlocProvider(
            providers: [
              BlocProvider<AuthBloc>.value(
                value: authBloc,
              ),
            ],
            child: const ProfilePage(),
          ),
        );
}

i use Flow Builder in Authentication

class AuthFlow extends StatelessWidget {
  const AuthFlow({super.key});
  @override
  Widget build(BuildContext context) {
    return FlowBuilder<AuthState>(
      state: context.select((AuthBloc state) => state.state),
      onGeneratePages: (projectState, pages) {
        if (projectState.status == AuthenticationStatus.authenticated) {
          context.read<ProjectBloc>().add(const GetListProject());
          context.read<NewsBloc>().add(const GetList());
        }
        return [
          if (projectState.status == AuthenticationStatus.authenticated)
            const MaterialPage<dynamic>(child: HomePage())
          else
            const MaterialPage<dynamic>(child: LoginPage()),
        ];
      },
    );
  }
}

But inside the HomePage when i go to the profile this error appears

Navigation Code Navigator.of(context).pushNamed(AppRouter.profile);

Error Code Navigator.onGenerateRoute was null, but the route named "/profile" was referenced. To use the Navigator API with named routes (pushNamed, pushReplacementNamed, or pushNamedAndRemoveUntil), the Navigator must be provided with an onGenerateRoute handler.

Is there another way to use the library with Generated Route?