sososdk / flash

⚡️A highly customizable, powerful and easy-to-use alerting library for Flutter.

Home Page:https://sososdk.github.io/flash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to use before having navigator in context

vasilich6107 opened this issue · comments

Hi @sososdk. Thanks for your work.
I'm trying to use this lib to display no connection popup
I need to place the Flash call before having navigator in context(i'm using auto_route lib)

MaterialApp.router(
        builder: (context, child) => Connection(child: child),
      )

I cannot use custom Overlay because Flash fails by trying to call to navigator
https://github.com/sososdk/flash/blob/master/lib/flash.dart#L94

Connection component pushes custom overlay into widget tree
At this moment there is no Navigator in context
So the Flash fails with error despite all requirements to operate are satisfied

Hi @vasilich6107 , you can get the context from navigatorKey.

/// Copy from: https://github.com/Milad-Akarie/auto_route_library/blob/e5ff8b91e1/auto_route/example/lib/mobile/main.dart
class _MyAppState extends State<MyApp> {
  final authService = AuthService();
  
  final navigatorKey = GlobalKey<NavigatorState>();

  late final _rootRouter = RootRouter(
      navigatorKey
      // authGuard: AuthGuard(),
      );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      theme: ThemeData.dark(),
      routerDelegate: _rootRouter.delegate(),
      routeInformationProvider: _rootRouter.routeInfoProvider(),
      routeInformationParser: _rootRouter.defaultRouteParser(),
      builder: (_, router) {
        return ChangeNotifierProvider<AuthService>(
          create: (_) => authService,
          child: BooksDBProvider(
            child: router!,
          ),
        );
      },
    );
  }
}

This could be a possible fix.
But isn't is easier and less verbose to make small Flash code adjustments?

#39 changed the meaning of 'persistent'.

When the persistent is false, we must ensure that there is an Overlay that is not rootOverlay.

actually there is still a check at line 100
I just added silencing of the error.

For now my code works, thanks for your help.
It is up to you whether to merge the PR or close it.

I'm closing this issue for now