1

The pagebuilder in GoRoute has the the only place I can grab the state.params, I want to update my StateNotifier when the route changes if it is different.

final LibraryRoutes = Provider<RouteBase>((ref) {
  return ShellRoute(
      navigatorKey: _LibraryKey,
      builder: (context, state, child) {
        return LibraryHomePage(
          child: child,
        );
      },
      routes: [
        //dashboard
        GoRoute(
            path: "/library/:LibraryKey/Dashboard",
            pageBuilder: (context, state) {
              final String passedValue = state.params['LibraryKey']!;
              final newLibrary = LibraryReadDto(LibraryKey: passedValue);

              //this line error's out because its during lifecycle method
              ref.read(AsyncLibraryProvidor.notifier).updateLibrary(newLibrary);

              final AsyncLibraryNotifier = ref.watch(AsyncLibraryProvidor);

              return AsyncLibraryNotifier.when(data: (data) {
                return NoTransitionPage(
                  child: Text("dashboard"),
                );
              }, error: (_, __) {
                return NoTransitionPage(
                  child: const Text("An error occurred"),
                );
              }, loading: () {
                return NoTransitionPage(
                  child: CircularProgressIndicator(),
                );
              });
            }),
      ]);

});

I've managed to put the update in a future to get around the problem is there a more elegant solution as the library is used in many different places.

GoRoute(
  path: "/library/:LibraryKey/Dashboard",
  pageBuilder: (context, state) {
    if (ref.read(LibraryProvider) == null) {
      final String passedValue = state.params['LibraryKey']!;
      try {
        //can't update during widget tree delay by 150 micoseconds so we can update after future
        Future.delayed(
          const Duration(microseconds: 150),
          () {
            final newLibrary = LibraryReadDto(LibraryKey: passedValue);
            ref.read(LibraryProvider.notifier).updateLibrary(newLibrary);
          },
        );
      } on Exception catch (ex) {
        print(ex.toString());
      }
    }
    return NoTransitionPage(
      child: Text("dashboard"),
    );
}),

1 Answers1

0

Right now It's not possible to use await within the page Builder, Write Some Logic in your page side to prefetch the values before the page renders.

IonicFireBaseApp
  • 925
  • 3
  • 10