Flutter main.dart initialRout is not Working

Issue

I’m New in this FrameWork and here initial Rout is not Accepting the Loggin Session value Please help me with this. I tried to add Home with the splash screen but that also not working I’m not getting What’s wrong in this.

This is my main Page

Future main() async {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark,
  ));
  runApp(Phoenix(child: AmericanCuisine()));
}

class AmericanCuisine extends StatefulWidget {
  @override
  _AmericanCuisineState createState() => _AmericanCuisineState();
}

class _AmericanCuisineState extends State<AmericanCuisine> {
  bool isLoggedIn;
  @override
  void initState() {
    super.initState();
    getData();
  }

  getData() async {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences storage = await SharedPreferences.getInstance();
    setState(() {
      isLoggedIn = storage.getBool("loggedIn");
    });
  }

  @override
  Widget build(BuildContext context) {
    
    return MultiBlocProvider(
      providers: [
        BlocProvider<LanguageCubit>(
          create: (context) => LanguageCubit(),
        ),
        BlocProvider<ThemeCubit>(
          create: (context) => ThemeCubit(),
        ),
      ],

in this page after using BlockBuilder how i To give the Initial Route

      child: BlocBuilder<ThemeCubit, ThemeData>(
        builder: (_, theme) {
          return BlocBuilder<LanguageCubit, Locale>(
            builder: (_, locale) {
              return MaterialApp(
                localizationsDelegates: [
                  const AppLocalizationsDelegate(),
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                ],
                supportedLocales: [
                  const Locale('en'),
                ],
                locale: locale,
                theme: theme,
                //This initial rout is not working.
                initialRoute: isLoggedIn == false ?'/': '/homeOrderAccount',                
                routes: {
                  // When navigating to the "/" route, build the FirstScreen widget.
                  '/': (context) => OpeningScreen(),
                  '/homeOrderAccount': (context) => HomeOrderAccount(),
                },
              );
            },
          );
        },
      ),
    );
  }
}

Solution

You can’t use initialRoute with routes map either delete ‘/’ from the routes map or delete the initialRoute:

Answered By – ahmetakil

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.