Home > OS >  Flutter landing page to navigate user depends on a state
Flutter landing page to navigate user depends on a state

Time:01-31

How can I navigate the user to the relevant screen? For example, If the user signed in, then the user must go homepage, but the user did not sign in, so the user must go login page. When I try to use navigator, then it gives this error:

FlutterError (setState() or markNeedsBuild() called during build. This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase. The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey#3ba8a] The widget which was currently being built when the offending call was made was: BlocBuilder<AuthCubit, AuthState>)

Also, I used auto route and auto route generator, but it gives an error is: The getter 'RouteInformationReportingType' isn't defined for the class 'AutoRouteInformationProvider'. package:auto_route/…/provider/auto_route_information_provider.dart:32

Can you help me?

error img

codes:

class LandingPage extends StatelessWidget {
  const LandingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocBuilder<AuthCubit, AuthState>(
        builder: (context, state) {
          if (state.isUserSignedIn) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const HomePage()),
            );
          } else {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SignInPage()),
            );
          }
          return Container();
        },
      ),
    );
  }
}

CodePudding user response:

I have updated flutter sdk to 2.8 and some syntax changes. That's it

CodePudding user response:

you have to change code like this, this will reder after all widget has been initialized, so it will navigate after initializing of all widgets.

if (state.isUserSignedIn) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const HomePage()),
            );
          } else {
            WidgetsBinding.instance!.addPostFrameCallback((_) {
               Navigator.push(
                 context,
                 MaterialPageRoute(builder: (context) => const SignInPage()),
               );
             });
          } 
  •  Tags:  
  • Related