skip to Main Content

I’m facing an issue when utilizing runApp() twice for initialization in a Flutter app. Here’s a simplified scenario of the problem:

void main() async {
  runApp(const SplashApp());
  await configuration();
  runApp(const ProviderScope(child: MainApp()));
}

class SplashApp extends StatelessWidget {
  const SplashApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: SplashPage(),
    );
  }
}


class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: goRouter,
    );
  }
}


final GoRouter goRouter = GoRouter(
  initialLocation: '/',
  routes: <RouteBase>[
//   GoRoute( ...
  ],
);

When using runApp() twice in this manner, the initial route information seems to be lost. Is there a way to preserve the initial route even after the asynchronous initialization steps?

I’ve tried capturing the initial route before the second runApp() call, but it doesn’t seem to maintain the routing context. How can I retrieve or maintain the initial route after performing these initialization tasks?

Any guidance or alternative approaches to handle this scenario would be greatly appreciated. Thank you!

Ref:

To resolve the issue, I attempted to capture and preserve the initial route information before performing the asynchronous tasks.
I did attempt a solution using the ModalRoute class to preserve the initial route context. However, despite trying various approaches involving the ModalRoute class and other potential solutions, I couldn’t resolve the issue of losing the initial route context after the second runApp() call.

2

Answers


  1. There is no need to use 2 different runApp().

    That’s why when you run you actual app it starts a new route
    You can make a Route class and add all you routings and whenever you navigate the routes update automatically that’s the easiest way.

    Check out how to set route class here

    Explore on the correct way on how to make a splash screen here

    Login or Signup to reply.
  2. Recustomize default splash screen using flutter native splash package link and remove the first run app

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search